我有一个有2个区域的Asp.net核心应用程序,每个区域都有自己的登录页面。我使用以下代码将用户重定向到登录页面:
services.AddAuthentication(opt => { opt.DefaultScheme = "AdminAuth";})
.AddCookie("UserAuth", opt => { opt.LoginPath = "/User/Login"; opt.AccessDeniedPath = "/User/AccessDenied"; })
.AddCookie("AdminAuth", opt => { opt.LoginPath = "/Identity/Account/Login"; opt.AccessDeniedPath = "/Admin/Dashboard/AccessDenied"; });提交登录表单后,用户成功登录,但没有转到控制器并返回登录页面。我将这些属性用于控制器:对于Admin:
[Authorize(Roles = "Admin", AuthenticationSchemes = "AdminAuth")]对于用户:
[Authorize(Roles = "User", AuthenticationSchemes = "UserAuth")]对于登录,我使用这个代码:
_signInManager.PasswordSignInAsync(model.Username, model.Password, false, lockoutOnFailure: false);发布于 2020-11-24 18:00:06
Identity封装了基本cookie身份验证方法。但是identity不提供对应于该方案的cookie设置。
我建议您使用cookie身份验证,并将cookie添加到指定的方案中。或者其中一家企业使用基本cookie身份验证。而另一家企业使用身份。
请确保在使用HttpContext.SignInAsync("schema",claimsPrincipal)时指明这些架构。否则,它将只生成您在启动时配置的默认模式的cookie。
这是一个用户登录的示例。
public IActionResult login()
{
string username = "username";
string userpassword = "password";
var userClaim = new List<Claim>
{
new Claim(ClaimTypes.Name,username),
new Claim(ClaimTypes.Role,"User")
};
var personIdentity = new ClaimsIdentity(userClaim, "identitycard");
var principle = new ClaimsPrincipal(new[] { personIdentity });
HttpContext.SignInAsync("UserAuth", principle);
return Redirect("resource");
}然后,将向浏览器写入两个cookie。

https://stackoverflow.com/questions/64982334
复制相似问题