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
-
I use "Authorize" attribute for the protected actions.Xiaowei.Jia– Xiaowei.Jia2015-08-09 13:34:00 +00:00Commented Aug 9, 2015 at 13:34
-
Have you checked the owin startup class? The login url is generally defined in the UseCookieAuthentication middleware.Brad Christie– Brad Christie2015-08-09 13:36:32 +00:00Commented 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: valueXiaowei.Jia– Xiaowei.Jia2015-08-09 13:43:32 +00:00Commented Aug 9, 2015 at 13:43
Add a comment
|
4 Answers
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");
});
}
Comments
app.UseCookieAuthentication(options =>
{
options.LoginPath = new PathString("/Admin/Login");
options.LogoutPath = new PathString("/Admin/LogOff");
},
IdentityOptions.ApplicationCookieAuthenticationScheme
);
6 Comments
Xiaowei.Jia
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()?
Joe Audette
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.
Bart Calixto
@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.
Serj Sagan
I Don't see any place to add:
IdentityOptions.ApplicationCookieAuthenticationSchemeBruceHill
The url posted by @Joe Audette is now as follows: github.com/aspnet/Identity/blob/dev/src/…
|
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
Andrew Grothe
For Core 1.1, this is the answer. I'm using a custom Identity provider and still works great.
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
Judah Gabriel Himango
I'm using Identity in AspNetCore 2, and this works for me. Thanks!