Search code examples
asp.netasp.net-coreasp.net-core-mvc.net-coreasp.net-authorization

Authentication and LoginPath for different areas in ASP.NET Core 2


ASP.NET Core 2

Help me to configure AddAuthentication for two routes: users (user accounts) and admin area.

For example, if user doesn't signed in and trying to enter /Account/Orders/ he'll be redirected to /Account/SignIn/.

But if someone trying access /Admin/Orders/ must be redireted to /Admin/Signin/

Have not found ay solution ATM.


Solution

  • Solved!

    In admin area (controllers) we using Authorize attr. arg.: [Authorize(AuthenticationSchemes = "backend")] and that is.

    BTW we are able to make any tuning by accessing HttpContext in AddCookie's options and events.

    Configuration:

    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/");
        });