简体   繁体   English

带有 Identity Core 和 Asp.net Core 3.1 的多重登录页面

[英]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.

相关问题 Asp.Net Core 3.1 Cookie 身份验证循环到登录页面 - Asp.Net Core 3.1 Cookie Authentication loop to login page ASP.NET Core 3.1 中的默认页面 - Default page in ASP.NET Core 3.1 使用 ASP.Net Core 3.1 进行身份验证 - 在未通过身份验证时,应用程序不会像在开发中那样重定向到生产环境中的登录 - Identity with ASP.Net Core 3.1 - on not authenticated, the app is not redirecting to Login in production like it does in dev 登录成功后传递用户详细信息来自身份asp.net core 3.1 - Pass user detail after Login Is successful From identity asp.net core 3.1 种子 ASP.NET .NET Core 3.1 中的身份用户和角色 - Seed ASP.NET Identity user and roles in .NET Core 3.1 ASP.NET Core 3.1 中基于角色的授权,带有身份和外部登录 - Role based authorization in ASP.NET Core 3.1 with Identity and ExternalLogin 如何使用 MongoDB 实现 ASP.NET Core 3.1 Identity? - How to implement ASP.NET Core 3.1 Identity with MongoDB? ASP.NET Core 3.1:Web API 身份登录 - ASP.NET Core 3.1: Web API identity sign in 使用身份在 Asp.Net Core 3.1 中配置 cookie - Configuring cookies in Asp.Net Core 3.1 with Identity 无法导航到身份注销页面-ASP.Net Core 3.1- - Can't Navigate to Identity Logout Page -ASP.Net Core 3.1-
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM