在Asp.Net中,对于具有多个身份验证方案的应用程序,需在HomeController中的构造函数中调用以下代码:
public HomeController(IHttpContextAccessor httpContextAccessor){
var schemeProvider = httpContextAccessor.HttpContext
.RequestServices.GetRequiredService
如果用户未获得重定向到LoginPath的URI,则需要改变以下DefaultForbidScheme或DefaultChallengeScheme属性的值:
schemeProvider.DefaultForbidScheme = YourForbidSchemeName; schemeProvider.DefaultChallengeScheme = YourLoginSchemeName;
此外,还可以使用AddCookie、AddOpenIdConnect等扩展方法支持多个认证方案。
例如,在Startup.cs的ConfigureServices方法中添加以下代码来启用Cookie和OpenIdConnect认证方案:
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.Cookie.Name = "YourAppCookieName"; }) .AddOpenIdConnect("oidc", options => { options.Authority = Configuration["Authority"]; options.ClientId = Configuration["ClientId"]; options.ClientSecret = Configuration["ClientSecret"]; options.ResponseType = "code"; options.SaveTokens = true; options.GetClaimsFromUserInfoEndpoint = true; });
这些代码片段可以用于解决Asp.Net多个身份验证方案中用户无法重定向到LoginPath URI的问题。