Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 769
Alex Avatar asked Aug 28 '17 19:08

Alex


People also ask

What is difference between authentication and authorization in ASP.NET Core?

Authentication is the process of determining a user's identity. Authorization is the process of determining whether a user has access to a resource. In ASP.NET Core, authentication is handled by the authentication service, IAuthenticationService, which is used by authentication middleware.

What is authentication and authorization in ASP.NET with example?

Authentication is knowing the identity of the user. For example, Alice logs in with her username and password, and the server uses the password to authenticate Alice. Authorization is deciding whether a user is allowed to perform an action. For example, Alice has permission to get a resource but not create a resource.


1 Answers

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/");
    });
like image 180
Alex Avatar answered Nov 15 '22 01:11

Alex