Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Setting the LoginPath not working int ASP.NET Core 2.0 #1414

Closed
anester opened this issue Sep 5, 2017 · 9 comments
Closed

Setting the LoginPath not working int ASP.NET Core 2.0 #1414

anester opened this issue Sep 5, 2017 · 9 comments

Comments

@anester
Copy link

anester commented Sep 5, 2017

        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.

@blowdart
Copy link
Member

blowdart commented Sep 6, 2017

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

@justin-ruffin
Copy link

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

image

@dkent600
Copy link

@justin-ruffin 's suggestion worked for me

@blowdart blowdart closed this as completed Oct 5, 2017
@develax
Copy link

develax commented Apr 19, 2018

Where is that stated in the documentation?

@worldbeater
Copy link

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

@justin-ruffin
Copy link

justin-ruffin commented Apr 21, 2018

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

@wutever0
Copy link

wutever0 commented Oct 15, 2018

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

@hadevnet
Copy link

hadevnet commented Nov 3, 2018

image

I commented .AddDefaultUI() and it worked!

@datvm
Copy link

datvm commented Nov 9, 2018

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:

            services
                .AddIdentityServer(options =>
                {
                    options.UserInteraction.LoginUrl = "/Identity/Account/Login";
                    options.UserInteraction.LogoutUrl = "/Identity/Account/Logout";
                })

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants