ASP.NET Core Identity change login URL

15,726

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;
                    }
                };

            });
Share:
15,726

Related videos on Youtube

Kyle
Author by

Kyle

.net software engineer, allergic to technical debt.

Updated on June 26, 2022

Comments

  • Kyle
    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/

  • Kyle
    Kyle almost 6 years
    Just tried both changes and it still returns to identity/account/login =/
  • Kyle
    Kyle almost 6 years
    I 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
    SVSchmidt over 4 years
    Yes, the order is what was my problem, too. Thanks.