问题描述
我的应用程序中有2种身份验证方法。 1对AD进行身份验证,1对常规登录。两种身份验证方法都使用Cookie。
AD身份验证是默认方案,为此,我使用CookieAuthenticationDefaults.AuthenticationScheme。 第二种cookie身份验证方案我称为externalLoginScheme。
services.AddAuthentication(options=> {
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie("externalLoginScheme")
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,options =>
{
...
})
.AddOpenIdConnect(options =>
{
...
})
每当我尝试对我的名为externalLoginScheme的方案进行signinasync()时,它都不会进行身份验证。
public async Task<IActionResult> Login()
{
var identity = new ClaimsIdentity("externalLoginScheme",ClaimTypes.Name,ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.Name,"superAdmin"));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier,"superAdmin"));
identity.AddClaim(new Claim(ClaimTypes.Role,"User"));
var principal = new ClaimsPrincipal(identity);
var authProperties = new AuthenticationProperties{
AllowRefresh = true,ExpiresUtc = DateTimeOffset.Now.AddDays(1),IsPersistent = true,};
await HttpContext.SignInAsync(
"externalLoginScheme",new ClaimsPrincipal(principal),authProperties
);
return RedirectToAction("");
}
IsAuthenticated始终为假。
public async Task<IActionResult> Index()
{
var test = this.HttpContext.User.Identity.IsAuthenticated;
return View();
}
但是,如果我将我的externalLoginScheme设置为默认方案,则验证有效。
services.AddAuthentication(options=> {
options.DefaultScheme = "externalLoginScheme";
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie("externalLoginScheme")
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,options =>
{
...
})
.AddOpenIdConnect(options =>
{
...
})
如何在不使用身份验证方案作为默认方案的情况下使此sighninasync()工作?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)