-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
331 changed files
with
64,742 additions
and
17,632 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,69 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using Azure.Identity; | ||
using Azure.Security.KeyVault.Secrets; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Azure.Identity; | ||
using Microsoft.Azure.Services.AppAuthentication; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace ConsoleStandaloneUsingAzureSecrets | ||
{ | ||
class Program | ||
{ | ||
private static IConfigurationRoot _config; | ||
private static IServiceProvider _services; | ||
namespace ConsoleStandaloneUsingAzureSecrets; | ||
|
||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Start Application and get key vault values"); | ||
class Program | ||
{ | ||
private static IConfigurationRoot _config; | ||
private static IServiceProvider _services; | ||
|
||
GetConfigurationsForEnvironment(); | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Start Application and get key vault values"); | ||
|
||
Console.WriteLine("Read Configurations"); | ||
GetConfigurationsForEnvironment(); | ||
|
||
SetupServices(); | ||
Console.WriteLine("Read Configurations"); | ||
|
||
Console.WriteLine("Services ready"); | ||
SetupServices(); | ||
|
||
// read config value | ||
var someSecret = _config["SomeSecret"]; | ||
Console.WriteLine("Services ready"); | ||
|
||
Console.WriteLine($"Read from configuration: {someSecret}"); | ||
Console.ReadLine(); | ||
} | ||
// read config value | ||
var someSecret = _config["SomeSecret"]; | ||
|
||
private static void SetupServices() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
Console.WriteLine($"Read from configuration: {someSecret}"); | ||
Console.ReadLine(); | ||
} | ||
|
||
// Do migration, seeding logic or whatever | ||
private static void SetupServices() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
_services = serviceCollection.BuildServiceProvider(); | ||
} | ||
// Do migration, seeding logic or whatever | ||
|
||
private static void GetConfigurationsForEnvironment() | ||
{ | ||
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | ||
var location = Assembly.GetEntryAssembly().Location; | ||
var directory = Path.GetDirectoryName(location); | ||
_services = serviceCollection.BuildServiceProvider(); | ||
} | ||
|
||
Console.WriteLine($"appsettings.json found"); | ||
Console.WriteLine($"{environmentName}"); | ||
private static void GetConfigurationsForEnvironment() | ||
{ | ||
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | ||
var location = Assembly.GetEntryAssembly().Location; | ||
var directory = Path.GetDirectoryName(location); | ||
|
||
var configBuilder = new ConfigurationBuilder() | ||
.AddJsonFile($"{directory}{Path.DirectorySeparatorChar}appsettings.json", false, true) | ||
.AddJsonFile($"{directory}{Path.DirectorySeparatorChar}appsettings.{environmentName}.json", true, true) | ||
.AddEnvironmentVariables(); | ||
_config = configBuilder.Build(); | ||
Console.WriteLine($"appsettings.json found"); | ||
Console.WriteLine($"{environmentName}"); | ||
|
||
var dnsNameKeyVault = _config["DNSNameKeyVault"]; | ||
var configBuilder = new ConfigurationBuilder() | ||
.AddJsonFile($"{directory}{Path.DirectorySeparatorChar}appsettings.json", false, true) | ||
.AddJsonFile($"{directory}{Path.DirectorySeparatorChar}appsettings.{environmentName}.json", true, true) | ||
.AddEnvironmentVariables(); | ||
_config = configBuilder.Build(); | ||
|
||
if (!string.IsNullOrWhiteSpace(dnsNameKeyVault)) | ||
{ | ||
var azureServiceTokenProvider = new AzureServiceTokenProvider(); | ||
configBuilder.AddAzureKeyVault(new Uri(dnsNameKeyVault), new DefaultAzureCredential()); | ||
var dnsNameKeyVault = _config["DNSNameKeyVault"]; | ||
|
||
if (!string.IsNullOrWhiteSpace(dnsNameKeyVault)) | ||
{ | ||
var azureServiceTokenProvider = new AzureServiceTokenProvider(); | ||
configBuilder.AddAzureKeyVault(new Uri(dnsNameKeyVault), new DefaultAzureCredential()); | ||
|
||
_config = configBuilder.Build(); | ||
} | ||
_config = configBuilder.Build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
namespace MvcHybridBackChannel | ||
{ | ||
public class AuthConfiguration | ||
{ | ||
public string StsServerIdentityUrl { get; set; } | ||
namespace MvcHybridBackChannel; | ||
|
||
public string Audience { get; set; } | ||
} | ||
public class AuthConfiguration | ||
{ | ||
public string StsServerIdentityUrl { get; set; } = string.Empty; | ||
public string Audience { get; set; } = string.Empty; | ||
} |
23 changes: 11 additions & 12 deletions
23
MvcHybridBackChannel/BackChannelLogout/BackchannelLogoutSession.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
namespace MvcHybridBackChannel.BackChannelLogout | ||
namespace MvcHybridBackChannel.BackChannelLogout; | ||
|
||
public partial class LogoutSessionManager | ||
{ | ||
public partial class LogoutSessionManager | ||
private class BackchannelLogoutSession | ||
{ | ||
private class BackchannelLogoutSession | ||
{ | ||
public string Sub { get; set; } | ||
public string Sid { get; set; } | ||
public string? Sub { get; set; } | ||
public string? Sid { get; set; } | ||
|
||
public bool IsMatch(string sub, string sid) | ||
{ | ||
return (Sid == sid && Sub == sub) || | ||
(Sid == sid && Sub == null) || | ||
(Sid == null && Sub == sub); | ||
} | ||
public bool IsMatch(string sub, string sid) | ||
{ | ||
return (Sid == sid && Sub == sub) || | ||
(Sid == sid && Sub == null) || | ||
(Sid == null && Sub == sub); | ||
} | ||
} | ||
} |
45 changes: 21 additions & 24 deletions
45
MvcHybridBackChannel/BackChannelLogout/CookieEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 41 additions & 45 deletions
86
MvcHybridBackChannel/BackChannelLogout/LogoutSessionManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,59 @@ | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace MvcHybridBackChannel.BackChannelLogout | ||
{ | ||
public partial class LogoutSessionManager | ||
{ | ||
private static readonly Object _lock = new Object(); | ||
private readonly ILogger<LogoutSessionManager> _logger; | ||
private IDistributedCache _cache; | ||
namespace MvcHybridBackChannel.BackChannelLogout; | ||
|
||
// Amount of time to check for old sessions. If this is to long, the cache will increase, | ||
// or if you have many user sessions, this will increase to much. | ||
private const int cacheExpirationInDays = 8; | ||
public partial class LogoutSessionManager | ||
{ | ||
private static readonly object _lock = new(); | ||
private readonly ILogger<LogoutSessionManager> _logger; | ||
private readonly IDistributedCache _cache; | ||
|
||
public LogoutSessionManager(ILoggerFactory loggerFactory, IDistributedCache cache) | ||
{ | ||
_cache = cache; | ||
_logger = loggerFactory.CreateLogger<LogoutSessionManager>(); | ||
} | ||
// Amount of time to check for old sessions. If this is to long, the cache will increase, | ||
// or if you have many user sessions, this will increase to much. | ||
private const int cacheExpirationInDays = 8; | ||
|
||
public void Add(string sub, string sid) | ||
{ | ||
_logger.LogWarning($"BC Add a logout to the session: sub: {sub}, sid: {sid}"); | ||
var options = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(cacheExpirationInDays)); | ||
public LogoutSessionManager(ILoggerFactory loggerFactory, IDistributedCache cache) | ||
{ | ||
_cache = cache; | ||
_logger = loggerFactory.CreateLogger<LogoutSessionManager>(); | ||
} | ||
|
||
lock (_lock) | ||
{ | ||
var key = sub + sid; | ||
var logoutSession = _cache.GetString(key); | ||
_logger.LogInformation($"BC logoutSession: {logoutSession}"); | ||
if (logoutSession != null) | ||
{ | ||
var session = JsonConvert.DeserializeObject<BackchannelLogoutSession>(logoutSession); | ||
} | ||
else | ||
{ | ||
var newSession = new BackchannelLogoutSession { Sub = sub, Sid = sid }; | ||
_cache.SetString(key, JsonConvert.SerializeObject(newSession), options); | ||
} | ||
} | ||
} | ||
public void Add(string sub, string sid) | ||
{ | ||
_logger.LogWarning("BC Add a logout to the session: sub: {sub}, sid: {sid}", sub, sid); | ||
var options = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(cacheExpirationInDays)); | ||
|
||
public async Task<bool> IsLoggedOutAsync(string sub, string sid) | ||
lock (_lock) | ||
{ | ||
_logger.LogInformation($"BC IsLoggedOutAsync: sub: {sub}, sid: {sid}"); | ||
var key = sub + sid; | ||
var matches = false; | ||
var logoutSession = await _cache.GetStringAsync(key); | ||
var logoutSession = _cache.GetString(key); | ||
_logger.LogInformation("BC logoutSession: {logoutSession}", logoutSession); | ||
if (logoutSession != null) | ||
{ | ||
var session = JsonConvert.DeserializeObject<BackchannelLogoutSession>(logoutSession); | ||
matches = session.IsMatch(sub, sid); | ||
_logger.LogInformation($"BC Logout session exists T/F {matches} : {sub}, sid: {sid}"); | ||
} | ||
else | ||
{ | ||
var newSession = new BackchannelLogoutSession { Sub = sub, Sid = sid }; | ||
_cache.SetString(key, JsonConvert.SerializeObject(newSession), options); | ||
} | ||
} | ||
} | ||
|
||
return matches; | ||
public async Task<bool> IsLoggedOutAsync(string sub, string sid) | ||
{ | ||
_logger.LogInformation("BC IsLoggedOutAsync: sub: {sub}, sid: {sid}", sub, sid); | ||
var key = sub + sid; | ||
var matches = false; | ||
var logoutSession = await _cache.GetStringAsync(key); | ||
if (logoutSession != null) | ||
{ | ||
var session = JsonConvert.DeserializeObject<BackchannelLogoutSession>(logoutSession); | ||
matches = session.IsMatch(sub, sid); | ||
_logger.LogInformation("BC Logout session exists T/F {matches} : {sub}, sid: {sid}", matches, sub, sid); | ||
} | ||
|
||
return matches; | ||
} | ||
} |
Oops, something went wrong.