Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Identity change login URL

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/

like image 707
Kyle Avatar asked May 31 '18 22:05

Kyle


5 Answers

I just ran into this same issue. I resolved it by moving my

services.ConfigureApplicationCookie call to after my services.AddIdentity call in ConfigureServices

like image 126
Paul Avatar answered Sep 21 '22 13:09

Paul


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();     } } 
like image 41
Austin Winstanley Avatar answered Sep 25 '22 13:09

Austin Winstanley


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.

like image 27
Sly Avatar answered Sep 22 '22 13:09

Sly


Move services.ConfigureApplicationCookie after services.AddIdentity and most important remove AddDefaultUI in services. Reference here

like image 34
Kelin Avatar answered Sep 22 '22 13:09

Kelin


 services.ConfigureApplicationCookie(options =>
            {
                options.Events = new CookieAuthenticationEvents
                {
                    OnRedirectToLogin = x =>
                    {
                        x.Response.Redirect("Account/Login");
                        return Task.CompletedTask;
                    }
                };

            });
like image 23
Pouria Kaveh Avatar answered Sep 25 '22 13:09

Pouria Kaveh