C# aspnet core 2.0的强类型OpenIdConnectOptions
我试图理解在构造OpenIDConnectOptions时如何利用强类型 我知道我们可以使用POCO类和IOptions实现为appsettings和其他项实现强类型,并从控制器构造函数访问这些项,但在这里,我的问题是在控制器部分之前。在运行时启动时失败 首先,我有startup.configureservice和:C# aspnet core 2.0的强类型OpenIdConnectOptions,c#,asp.net-core-2.0,asp.net-core-identity,C#,Asp.net Core 2.0,Asp.net Core Identity,我试图理解在构造OpenIDConnectOptions时如何利用强类型 我知道我们可以使用POCO类和IOptions实现为appsettings和其他项实现强类型,并从控制器构造函数访问这些项,但在这里,我的问题是在控制器部分之前。在运行时启动时失败 首先,我有startup.configureservice和: services.AddAzureADOpenIDAuthentication(Configuration); 我有用于AddAzureADOpenIDAuthenticatio
services.AddAzureADOpenIDAuthentication(Configuration);
我有用于AddAzureADOpenIDAuthentication的IServiceCollection的扩展方法,如:
services.Configure<AzureADOptions>(configuration.GetSection("Authentication:AzureAd"));
services.AddSingleton<IOptionsMonitor<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsSetup>();
services.AddAuthentication(auth =>
{
auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie().AddOpenIdConnect();
return services;
services.Configure(configuration.GetSection(“Authentication:AzureAd”);
services.AddSingleton();
services.AddAuthentication(auth=>
{
auth.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie().AddOpenIdConnect();
返回服务;
最后,我使用以下IOptionsMonitor实现了AzureADOpenIdConnectionOptions设置:
public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions>
{
public OpenIdConnectOptions CurrentValue { get; set; }
public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureADOptions> azureADOptions)
{
CurrentValue = new OpenIdConnectOptions();
CurrentValue.ClientId = azureADOptions.CurrentValue.ClientId;
CurrentValue.Authority = azureADOptions.CurrentValue.Authority;
CurrentValue.CallbackPath = azureADOptions.CurrentValue.CallbackPath;
}
public OpenIdConnectOptions Get(string name)
{
return CurrentValue;
}
public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener)
{
throw new NotImplementedException();
}
}
public static class AzureAdAuthenticationBuilderExtensions
{
public static AuthenticationBuilder AddAzureADOpenIDAuthentication(this AuthenticationBuilder builder, IConfiguration configuration)
{
builder.Services.Configure<AzureAdOptions>(configuration.GetSection("AzureAd"));
builder.Services.AddSingleton<IOptionsMonitor<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsSetup>();
builder.Services.AddAuthentication(auth =>
{
auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect();
return builder;
}
public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions>
{
public OpenIdConnectOptions CurrentValue { get; set; }
private IDataProtectionProvider _dataProtectionProvider;
public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureAdOptions> azureADOptions,IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider = dataProtectionProvider;
CurrentValue = new OpenIdConnectOptions
{
ClientId = azureADOptions.CurrentValue.ClientId,
Authority = $"{azureADOptions.CurrentValue.Instance}{azureADOptions.CurrentValue.TenantId}",
CallbackPath = azureADOptions.CurrentValue.CallbackPath
};
}
public OpenIdConnectOptions Get(string name)
{
OpenIdConnectPostConfigureOptions op = new OpenIdConnectPostConfigureOptions(_dataProtectionProvider);
op.PostConfigure(name, CurrentValue);
return CurrentValue;
}
public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener)
{
throw new NotImplementedException();
}
}
}
公共类AzureadOpen连接选项设置:IOPTIONS监视器
{
公共OpenIdConnectOptions当前值{get;set;}
公共AzureA开放连接选项设置(IOPTIONS监视AzureA采用)
{
CurrentValue=新的OpenIdConnectOptions();
CurrentValue.ClientId=azureADOptions.CurrentValue.ClientId;
CurrentValue.Authority=azureADOptions.CurrentValue.Authority;
CurrentValue.CallbackPath=azureADOptions.CurrentValue.CallbackPath;
}
公共OpenIdConnectOptions获取(字符串名称)
{
返回当前值;
}
公共IDisposable OnChange(操作侦听器)
{
抛出新的NotImplementedException();
}
}
当我运行这段代码时,它两次命中构造函数和OpenIdConnectOptions,并通过构造函数级别的断点,检查设置是否正确地从azureADOptions传输到OpenIdConnectOptions CurrentValue。
尽管如此,我还是收到了一条错误消息(在我按下login之前,它意味着自己启动它)
InvalidOperationException:向OpenIdConnectOptions提供权限、元数据地址、配置或配置管理器
我不确定是否正确实现了OpenIdConnectOptions Get(字符串名)。
还有一个疑问是,我应该如何实现OnChange(Action listener)来监听appsettings.json的运行时更改。要返回
OpenIdConnectOptions
,需要初始化ConfigurationManager,并编写如下简单代码:
public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions>
{
public OpenIdConnectOptions CurrentValue { get; set; }
public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureADOptions> azureADOptions)
{
CurrentValue = new OpenIdConnectOptions();
CurrentValue.ClientId = azureADOptions.CurrentValue.ClientId;
CurrentValue.Authority = azureADOptions.CurrentValue.Authority;
CurrentValue.CallbackPath = azureADOptions.CurrentValue.CallbackPath;
}
public OpenIdConnectOptions Get(string name)
{
return CurrentValue;
}
public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener)
{
throw new NotImplementedException();
}
}
public static class AzureAdAuthenticationBuilderExtensions
{
public static AuthenticationBuilder AddAzureADOpenIDAuthentication(this AuthenticationBuilder builder, IConfiguration configuration)
{
builder.Services.Configure<AzureAdOptions>(configuration.GetSection("AzureAd"));
builder.Services.AddSingleton<IOptionsMonitor<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsSetup>();
builder.Services.AddAuthentication(auth =>
{
auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect();
return builder;
}
public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions>
{
public OpenIdConnectOptions CurrentValue { get; set; }
private IDataProtectionProvider _dataProtectionProvider;
public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureAdOptions> azureADOptions,IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider = dataProtectionProvider;
CurrentValue = new OpenIdConnectOptions
{
ClientId = azureADOptions.CurrentValue.ClientId,
Authority = $"{azureADOptions.CurrentValue.Instance}{azureADOptions.CurrentValue.TenantId}",
CallbackPath = azureADOptions.CurrentValue.CallbackPath
};
}
public OpenIdConnectOptions Get(string name)
{
OpenIdConnectPostConfigureOptions op = new OpenIdConnectPostConfigureOptions(_dataProtectionProvider);
op.PostConfigure(name, CurrentValue);
return CurrentValue;
}
public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener)
{
throw new NotImplementedException();
}
}
}
公共静态类AzureAdAuthenticationBuilderExtensions
{
公共静态身份验证生成器AddAzureadOpen身份验证(此身份验证生成器,IConfiguration配置)
{
builder.Services.Configure(configuration.GetSection(“AzureAd”);
builder.Services.AddSingleton();
builder.Services.AddAuthentication(auth=>
{
auth.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect();
返回生成器;
}
公共类AzureadOpen连接选项设置:IOPTIONS监视器
{
公共OpenIdConnectOptions当前值{get;set;}
私有IDataProtectionProvider_dataProtectionProvider;
公共AzureADOpenIdConnectionOptions设置(IOptionsMonitor azureADOptions,IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider=dataProtectionProvider;
CurrentValue=新的OpenIdConnectOptions
{
ClientId=azureADOptions.CurrentValue.ClientId,
Authority=$“{azureADOptions.CurrentValue.Instance}{azureADOptions.CurrentValue.TenantId}”,
CallbackPath=azureADOptions.CurrentValue.CallbackPath
};
}
公共OpenIdConnectOptions获取(字符串名称)
{
OpenIdConnectPostConfigureOptions op=新的OpenIdConnectPostConfigureOptions(\u dataProtectionProvider);
op.PostConfigure(名称、当前值);
返回当前值;
}
公共IDisposable OnChange(操作侦听器)
{
抛出新的NotImplementedException();
}
}
}
要返回OpenIdConnectOptions
,您需要初始化ConfigurationManager,并使用如下简单代码:
public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions>
{
public OpenIdConnectOptions CurrentValue { get; set; }
public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureADOptions> azureADOptions)
{
CurrentValue = new OpenIdConnectOptions();
CurrentValue.ClientId = azureADOptions.CurrentValue.ClientId;
CurrentValue.Authority = azureADOptions.CurrentValue.Authority;
CurrentValue.CallbackPath = azureADOptions.CurrentValue.CallbackPath;
}
public OpenIdConnectOptions Get(string name)
{
return CurrentValue;
}
public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener)
{
throw new NotImplementedException();
}
}
public static class AzureAdAuthenticationBuilderExtensions
{
public static AuthenticationBuilder AddAzureADOpenIDAuthentication(this AuthenticationBuilder builder, IConfiguration configuration)
{
builder.Services.Configure<AzureAdOptions>(configuration.GetSection("AzureAd"));
builder.Services.AddSingleton<IOptionsMonitor<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsSetup>();
builder.Services.AddAuthentication(auth =>
{
auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect();
return builder;
}
public class AzureADOpenIdConnectOptionsSetup : IOptionsMonitor<OpenIdConnectOptions>
{
public OpenIdConnectOptions CurrentValue { get; set; }
private IDataProtectionProvider _dataProtectionProvider;
public AzureADOpenIdConnectOptionsSetup(IOptionsMonitor<AzureAdOptions> azureADOptions,IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider = dataProtectionProvider;
CurrentValue = new OpenIdConnectOptions
{
ClientId = azureADOptions.CurrentValue.ClientId,
Authority = $"{azureADOptions.CurrentValue.Instance}{azureADOptions.CurrentValue.TenantId}",
CallbackPath = azureADOptions.CurrentValue.CallbackPath
};
}
public OpenIdConnectOptions Get(string name)
{
OpenIdConnectPostConfigureOptions op = new OpenIdConnectPostConfigureOptions(_dataProtectionProvider);
op.PostConfigure(name, CurrentValue);
return CurrentValue;
}
public IDisposable OnChange(Action<OpenIdConnectOptions, string> listener)
{
throw new NotImplementedException();
}
}
}
公共静态类AzureAdAuthenticationBuilderExtensions
{
公共静态身份验证生成器AddAzureadOpen身份验证(此身份验证生成器,IConfiguration配置)
{
builder.Services.Configure(configuration.GetSection(“AzureAd”);
builder.Services.AddSingleton();
builder.Services.AddAuthentication(auth=>
{
auth.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
auth.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
auth.DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect();
返回生成器;
}
公共类AzureadOpen连接选项设置:IOPTIONS监视器
{
公共OpenIdConnectOptions当前值{get;set;}
私有IDataProtectionProvider_dataProtectionProvider;
公共AzureADOpenIdConnectionOptions设置(IOptionsMonitor azureADOptions,IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider=dataProtectionProvider;
CurrentValue=新的OpenIdConnectOptions
{
ClientId=azureADOptions.CurrentValue.Client