下面是我的program.cs类。我将 Azure 应用程序详细信息(例如 clientid、secret 和tenant id)放置在 appsettings.json 文件中。当我们最初运行应用程序时,它工作正常,但我希望当我们编辑 appsettings.json 文件中的应用程序详细信息时,它开始接受新应用程序而无需重新启动。目前只有重启后应用程序更改才会生效。
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Extensions.Options;
using OpenIDConnect.Models;
using OpenIDConnect.Services;
var builder = WebApplication.CreateBuilder(args);
// Add configuration
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
//.AddJsonFile("azureAdConfig.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
var configurationRoot = configurationBuilder.Build();
builder.Configuration.AddConfiguration(configurationRoot);
// Register services
builder.Services.Configure<AzureAdOptions>(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddSingleton<IConfigureOptions<AzureAdOptions>, ConfigureAzureAdOptions>();
builder.Services.AddSingleton<JsonConfigService>();
builder.Services.AddSingleton<CustomOpenIdConnectService>();
// Configure authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme) // Cookie-based authentication
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
var serviceProvider = builder.Services.BuildServiceProvider();
var azureAdOptionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<AzureAdOptions>>();
var azureAdOptions = azureAdOptionsMonitor.CurrentValue;
// Set initial values
options.ClientId = azureAdOptions.ClientId;
options.Authority = $"https://login.microsoftonline.com/{azureAdOptions.TenantId}/v2.0";
options.ClientSecret = azureAdOptions.ClientSecret;
// Additional configuration
options.ResponseType = "code";
options.SaveTokens = true;
options.UsePkce = true;
options.Scope.Add("offline_access");
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Events.OnRedirectToIdentityProvider = context =>
{
var serviceProvider = builder.Services.BuildServiceProvider();
var azureAdOptionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<AzureAdOptions>>();
var azureAdOptions = azureAdOptionsMonitor.CurrentValue;
var openIdConnectService = serviceProvider.GetRequiredService<CustomOpenIdConnectService>();
// Invalidate the cache to force a refresh
openIdConnectService.InvalidateMetadata(azureAdOptions.TenantId, azureAdOptions.ClientId, azureAdOptions.ClientSecret, azureAdOptions.Domain);
context.Options.ClientId = azureAdOptions.ClientId;
context.Options.Authority = $"https://login.microsoftonline.com/{azureAdOptions.TenantId}/v2.0";
context.Options.ClientSecret = azureAdOptions.ClientSecret;
context.ProtocolMessage.DomainHint = azureAdOptions.Domain;
return Task.CompletedTask;
};
});
builder.Services.AddAuthorization();
builder.Services.AddRazorPages();
var app = builder.Build
有 .net 配置来实现这一点。您可以尝试
IOptionsMonitor
实时响应配置更改,而无需重新启动应用程序。
应用程序设置
{
"AzureAd": {
"ClientId": "xxx",
"ClientSecret": "xxx",
"TenantId": "xxx"
}
}
你需要一个选项课程
public class AzureAdOptions
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string TenantId { get; set; }
}
注册服务以供进一步使用
builder.Services.Configure<AzureAdOptions>
builder.Services.AddSingleton<IAzureAdService, AzureAdService>();
然后就可以通过服务访问配置了
public class SomeService{
private readonly IOptionsMonitor<AzureAdOptions> _azureAdOptions;
public SomeService(IOptionsMonitor<AzureAdOptions> azureAdOptions)
{
_azureAdOptions = azureAdOptions;
}
...
}