9

I am using asp.net 5 and Identity 3 to authenticate users but it always redirect to the default login url which is "Account/Login". I want to change it but there does not seem to be anywhere to set this options. I use AddIdentity() in Configure() method. Please help. Thanks

3
  • I use "Authorize" attribute for the protected actions. Commented Aug 9, 2015 at 13:34
  • Have you checked the owin startup class? The login url is generally defined in the UseCookieAuthentication middleware. Commented Aug 9, 2015 at 13:36
  • Yes, I am trying this: app.UseCookieAuthentication(options => options.LoginPath = new PathString("Admin/Login")); but get an error: System.ArgumentException Parameter name: value Commented Aug 9, 2015 at 13:43

4 Answers 4

12

Using .Net Core 1.0.0 + Identity + Facebook OAuth, the accepted answer no longer compiles. This is what worked:

public void ConfigureServices(IServiceCollection services)
{
    (...)    
    services.Configure<IdentityOptions>(options =>
    {
        options.Cookies.ApplicationCookie.LoginPath = new PathString("/Login");
        options.Cookies.ApplicationCookie.LogoutPath = new PathString("/Logoff");
    });
}
Sign up to request clarification or add additional context in comments.

Comments

9
app.UseCookieAuthentication(options =>
{
    options.LoginPath = new PathString("/Admin/Login");
    options.LogoutPath = new PathString("/Admin/LogOff");
},
IdentityOptions.ApplicationCookieAuthenticationScheme
);

6 Comments

Thanks. Most of the time developers will use customised login URL. If this is the code to customise login url and set cookie authentication, what's the point of UseIdenitty()?
those extension methods are just meant to be helpful and convenient, you don't have to use them. I suspect most people will just use the provided Account and Manage controllers that are provided in the project template, it is only natural that they wire up the path to Account controller since that is what they provided. The extension methods do hide some things that people may want to change. You can either wire up everything you need yourself or you can call .UseIdentity and then after that wire up your own things to override it or you can leave out .UseIdentity and just do it all yourself.
@Xiaowei.Jia has a great point. You may suggest (mvc is open source and accept suggestions via GitHub for things like this) that UseIdentity supports an options param for common customization. so you can end up with something like UseIdentity(opt=> opt.LoginPath = ...) and other common options.
I Don't see any place to add: IdentityOptions.ApplicationCookieAuthenticationScheme
The url posted by @Joe Audette is now as follows: github.com/aspnet/Identity/blob/dev/src/…
|
3

With ASP.NET Core 1.1 + Identity I use this :

public void ConfigureServices(IServiceCollection services)
{
   (...)
   services.AddIdentity<ApplicationUser, IdentityRole>(x =>
   {
      x.Cookies.ApplicationCookie.LoginPath = new PathString("/Admin/Login");
      x.Cookies.ApplicationCookie.LogoutPath = new PathString("/Admin/LogOff");
   }
}

1 Comment

For Core 1.1, this is the answer. I'm using a custom Identity provider and still works great.
2

With ASP.NET Core 2.0 + Identity, this has changed to:

services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/LogIn");

More on migrating to 2.0 here.

1 Comment

I'm using Identity in AspNetCore 2, and this works for me. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.