ASP.NET Core 2中不同区域的身份验证和LoginPath

Ale*_*lex 4 asp.net asp.net-authorization asp.net-core-mvc .net-core asp.net-core

ASP.NET Core 2

帮我配置AddAuthentication两条路由:用户(用户帐户)和管理区域。

例如,如果用户未登录并尝试输入,则将/Account/Orders/其重定向到/Account/SignIn/

但是,如果尝试访问的人/Admin/Orders/必须重新分配给/Admin/Signin/

尚未找到解决方案的ATM。

Ale*_*lex 5

解决了!

admin区域(控制器)中,我们使用Authorize attr。arg .:[Authorize(AuthenticationSchemes = "backend")]就是这样。

顺便说一句,我们能够通过访问AddCookie的选项和事件中的HttpContext进行任何调整。

组态:

services
    .AddAuthentication(o =>
    {
        o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
    {
        o.LoginPath = new PathString("/account/login/");
    })
    .AddCookie("backend", o =>
    {
        o.LoginPath = new PathString("/admin/account/login/");
    });
Run Code Online (Sandbox Code Playgroud)