Where do I define the url for an aspnetcore authorisation fail?

Viewed 249

So I've got aspnet identity razor pages in my asp.net core 3+ project.

When I click on login or register defaults it directs me to a url starting with Identity e.g.

https://localhost:12345/Identity/Account/Login

All of the navigations to the other pages, account management etc. work fine giving me this Identity prefix.

However, I added some userRoles and am using the Authorize(Roles = "Admin") attribute on my MVC Admin controller, so when I try to access the index page on this controller without signing in, it's trying to redirect me to login, with a returnURL in the querystring

but, the URL is missing the Identity prefix and sends me to:

https://localhost:12345/Account/Login?ReturnUrl=%2Fadmin

Where do I change this? I have no idea where this automatic redirection on authorisation failure is?!


EDIT: I'm thinking it may have something to do with the startup bit declaring endpoints:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

Not sure how I can alter that to make a default login path

2 Answers

the URL is missing the Identity prefix and sends me to:

https://localhost:12345/Account/Login?ReturnUrl=%2Fadmin

If you set LoginPath with "/Account/Login" in ConfigureServices method of Startup.cs like below, which would cause above issue.

services.ConfigureApplicationCookie(options =>
{
    //options here

    options.LoginPath = "/Account/Login";

    //...
});
Related