How to make the Login page become a standard route for an Asp.Net Core 2.2 application

Asked

Viewed 191 times

0

The Login page is within the following folders Area - Identity - Pages - Account.

How to make the Login Page become a standard route, this is when the user accesses the Asp.Net Core 2.2 application go straight to the login page.

The default route at the moment is this below.

routes.MapRoute(
                 name: "default",
                 template: "{controller=Home}/{action=Index}/{id?}");

I tried like this and it didn’t work.

Routes.Maproute( name: "default", template: "Identity/{controller=Account}/{action=Login}/{id?}");

  • Managed to solve?

  • @Leandroangelo to tell the truth did not understand his answer. In Startup.Cs we can configure routes, I want login to be the home page of my application, I tried it like this and it didn’t work : Routes.Maproute( name: "default", template:?}");

  • Why is the login page the home? The user will be able to continue browsing even without logging in?

1 answer

2


It’s not about route: it’s about making all controllers protected by a standard authorization filter. Once all controllers are protected, all access will be redirected to the login page. Controllers can only be accessed after logging in.

To do this, change the method ConfigureServices in your class Startup:

Of:

services.AddMvc();

To:

services.AddMvc(options => {
    var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});

If you want some control to be displayed by non-authenticated users, just use the annotation [AllowAnonymous] as usual:

[AllowAnonymous]
public IActionResult Index() => View();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.