为asp.net-core 2中的每个角色实现不同的登录页面

moh*_*her 0 httpcookie asp.net-core asp.net-core-2.0

我想根据每个用户在 asp net core 中的角色实现不同的登录页面。我可以设置登录路径,但它对于任何角色都是静态的。

   services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            options.LoginPath = "Account/Login/";
            options.AccessDeniedPath = "Account/Forbidden/";
        }); 
Run Code Online (Sandbox Code Playgroud)

因此,当我调用授权(role =“Admin”)的操作时,重定向到管理员登录页面。当调用 Authorize(role="User") 重定向到用户登录页面的操作时

moh*_*her 5

我在启动ConfigureServices时添加了两种不同的身份验证方案,如下所示

services.AddAuthentication(options =>
            {
                options.DefaultScheme = "UserAuth";
            })
            .AddCookie("UserAuth", options =>
      {
          options.LoginPath = "/Account/Login/";
          options.AccessDeniedPath = "/Account/AccessDenied/";

      })
       .AddCookie("AdminAuth", options =>
       {
           options.LoginPath = "/Admin/Account/Login/";
           options.AccessDeniedPath = "/Admin/Account/AccessDenied/";

       });
Run Code Online (Sandbox Code Playgroud)

当使用管理员角色控制器授权时,我选择管理方案

[Authorize(Roles = "Administrator", AuthenticationSchemes = "AdminAuth")]
Run Code Online (Sandbox Code Playgroud)

当使用用户角色控制器授权时,我选择用户方案

 [Authorize(Roles = "User", AuthenticationSchemes = "UserAuth")]
Run Code Online (Sandbox Code Playgroud)

您可以查看此链接 如何在 ASP.NET Core 2.0 中设置多个身份验证方案?