[英]Multiple Login Page with Identity Core and Asp.net Core 3.1
I Have an Asp.net Core Application with 2 areas, each area has its own login page.我有一个包含 2 个区域的 Asp.net Core 应用程序,每个区域都有自己的登录页面。 I use this code to redirect the user to the login page :
我使用此代码将用户重定向到登录页面:
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"; });
after submitting the login form, the user login successfully but didn't go to the controller and come back to the login page.提交登录表单后,用户登录成功但没有进入控制器并返回登录页面。 I Use these attributes for controllers : for Admin :
我将这些属性用于控制器:用于管理员:
[Authorize(Roles = "Admin", AuthenticationSchemes = "AdminAuth")]
for Users :对于用户:
[Authorize(Roles = "User", AuthenticationSchemes = "UserAuth")]
and for login, I am using this code :对于登录,我正在使用此代码:
_signInManager.PasswordSignInAsync(model.Username, model.Password, false, lockoutOnFailure: false);
Identity encapsulates the basic cookie authentication method. Identity 封装了基本的 cookie 认证方法。 But identity does not provide cookie settings corresponding to the scheme.
但是身份不提供与方案对应的cookie设置。
I suggest you use cookie authentication, and add cookies to the specified scheme.建议使用cookie认证,并在指定的scheme中添加cookie。 Or one of the businesses uses basic cookie authentication.
或者其中一家企业使用基本的 cookie 身份验证。 While another business uses identity.
而另一家企业使用身份。
Please make sure to indicate these schemas when use HttpContext.SignInAsync("schema",claimsPrincipal)
.请确保在使用
HttpContext.SignInAsync("schema",claimsPrincipal)
时指明这些架构。 Otherwise, it will only generate the default schema's cookie which you configure in startup.否则,它只会生成您在启动时配置的默认架构 cookie。
This is an example of user login.这是用户登录的示例。
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");
}
Then, two cookies will be written to the browser.然后,两个 cookie 将被写入浏览器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.