ASP.NET Core Identity change login URL
Solution 1
I just ran into this same issue. I resolved it by moving my
services.ConfigureApplicationCookie
call to after my services.AddIdentity
call in ConfigureServices
Solution 2
Try adding new PathString("...")
and setting the route in the controller.
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = new PathString("/Account/AccessDenied");
options.Cookie.Name = "Cookie";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(720);
options.LoginPath = new PathString("/Account/Login");
options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
options.SlidingExpiration = true;
});
[AllowAnonymous]
[Route("Account")]
public class SecurityController : Controller
{
[Route("Login/{returnUrl?}")]
public IActionResult Login(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
}
Solution 3
Check how you register Identity service if you have:
services.AddDefaultIdentity<IdentityUser>(options => { })
.AddEntityFrameworkStores<ApplicationDbContext>();
replace it with
services.AddIdentity<IdentityUser, IdentityRole>(options => { })
.AddEntityFrameworkStores<ApplicationDbContext>();
and keep your code in the ConfigureApplicationCookie
that worked in my case.
Solution 4
Move services.ConfigureApplicationCookie after services.AddIdentity and most important remove AddDefaultUI in services. Reference here
Solution 5
services.ConfigureApplicationCookie(options =>
{
options.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = x =>
{
x.Response.Redirect("Account/Login");
return Task.CompletedTask;
}
};
});
Related videos on Youtube
Comments
-
Kyle almost 2 years
I'm using ASP.NET Core 2.1 and I used scaffolding to add Identity, which is working OK Except that when I try to go to a page that requires login, it takes me to:
/Identity/Account/Login?ReturnUrl
How do I change it to go to just /Account/Login which is my own login page i created.
I tried this:
services.ConfigureApplicationCookie(options => { options.AccessDeniedPath = "/Account/AccessDenied"; options.Cookie.Name = "Cookie"; options.Cookie.HttpOnly = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(720); options.LoginPath = "/Account/Login"; options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter; options.SlidingExpiration = true; });
but it still goes to /Identity/
-
eugen over 3 yearsfor core 3.1 it's a different answer...i ended up here because i searched with 'asp.net core' term on google without specific version; stackoverflow.com/questions/58525775/…
-
-
Kyle almost 6 yearsJust tried both changes and it still returns to identity/account/login =/
-
Kyle almost 6 yearsI got around this for the time being by having my identity/account/login page redirect to /account/login but if you have any other ideas on how to properly fix that, I'd rather not leave that redirect in the project
-
SVSchmidt over 4 yearsYes, the order is what was my problem, too. Thanks.