Identity: Setting the LoginPath not working int ASP.NET Core 2.0

Created on 5 Sep 2017  路  9Comments  路  Source: aspnet/Identity

        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = "/Login/Index";
            options.ReturnUrlParameter = "RedirectUrl";
            options.LogoutPath = "/Login/Logout";
            options.AccessDeniedPath = "/Login/Index";
            options.ExpireTimeSpan = new TimeSpan(0, 15, 0);

        });

After setting this "/Account/Login?RedirectUrl=%2F" is being used. I can manually go to the login page and login. Is there another options I need to set now? I can see that it is using ReturnUrlParameter and ExpireTimeSpan.

Most helpful comment

Try configuring "Services.AddIdentity" then "Services.ConfigureApplicationCookie" as shown below.

image

All 9 comments

How is the challenge triggering? Is it just via [Authorize]? Can you show us all your config?

Try configuring "Services.AddIdentity" then "Services.ConfigureApplicationCookie" as shown below.

image

@justin-ruffin 's suggestion worked for me

Where is that stated in the documentation?

Why does the order affect behavior?
This isn't obvious at all and is driving me insane.

The reason the order matters is because "AddIdentity" configures the application cookie.

If you call "ConfigureApplicationCookie" prior to "AddIdentity" then your custom configuration is overwritten.

See Below:

image

Hi I still facing this issue even after placing ConfigureApplicationCookie after AddDefaultIdentity.
Any suggestions?

Here is my ConfigureServices:

>
>
>

public void ConfigureServices(IServiceCollection services)
{

        services.Configure<CookiePolicyOptions>(options =>
        {
                     options.CheckConsentNeeded = context => true;
                     options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddSingleton<IDatabaseFactory, DatabaseFactory>();
        services.AddTransient<ConferencesRepository>();
        services.AddTransient<IUserStore<IdentityUser>, UserStore>();
        services.AddTransient<IEmailSender, EmailSender>(s =>
            new EmailSender(
                Configuration["EmailSender:Host"],
                     .....
                Configuration["EmailSender:Password"]
            )
        );
        services.AddDefaultIdentity<IdentityUser>();

        services.Configure<IdentityOptions>(options =>
        {
            // Password settings.
            options.Password.RequireDigit = false;
                     .....
            options.User.RequireUniqueEmail = false;
        });

        services.ConfigureApplicationCookie(options => {
            options.LoginPath = new PathString("/Account/LoginRegister");
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

Forgot to mention that I am using ASP.NET Core 2.1.1

image

I commented .AddDefaultUI() and it worked!

Hi, anyone could do it with ASP.NET IdentityServer4? I have set it after adding Identity and even after IdentityServer configuration (I also tried before too), and removed the AddDefaultUI, but it still redirects me to /account/login. Here is the code:

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(apiSettings.ConnectionStrings.BibliIdContext));
            services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
                //.AddDefaultUI();  

            var migrationsAssembly = typeof(Startup).Assembly.GetName().Name;

            services
                .AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                    {
                        builder.UseSqlServer(
                            apiSettings.ConnectionStrings.BibliIdContext,
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                    };
                })
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                    {
                        builder.UseSqlServer(
                            apiSettings.ConnectionStrings.BibliIdContext,
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                    };

                    options.EnableTokenCleanup = true;
                })
                .AddAspNetIdentity<IdentityUser>();

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = "/Identity/Account/LoginTest";
                options.LogoutPath = "/Identity/Account/Logout";
            });


            services
                .AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

EDIT: Founded the solution from IdentityServer's documentation. For those who search for it later:

                .AddIdentityServer(options =>
                {
                    options.UserInteraction.LoginUrl = "/Identity/Account/Login";
                    options.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
                })
Was this page helpful?
0 / 5 - 0 ratings