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/
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.
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();
}
}
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;
}
};
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.