Reputation: 5313
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
Upvotes: 2
Views: 691
Reputation: 27793
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";
//...
});
Upvotes: 1
Reputation: 18290
Refer these:
ASP.NET - Redirect to Error Page if Roles Authorization Fails
ASP.NET CORE 2.2 authorization error redirect page
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = "/YourCustomAccessDeniedPath";
});
}
or
Configuring IdentityOptions in Startup as below,
services.Configure<IdentityOptions>(opt =>
{
opt.Cookies.ApplicationCookie.LoginPath = new PathString("/yourcustompage");
});
Upvotes: 1