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/
I just ran into this same issue. I resolved it by moving my
services.ConfigureApplicationCookie
call to after my services.AddIdentity
call in ConfigureServices
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(); } }
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.
Move services.ConfigureApplicationCookie after services.AddIdentity and most important remove AddDefaultUI in services. Reference here
services.ConfigureApplicationCookie(options =>
{
options.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = x =>
{
x.Response.Redirect("Account/Login");
return Task.CompletedTask;
}
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With