ASP.Net Core application redirecting to /Account/Login even after implementing IdentityServer

Viewed 984

I have created a simple Identity Server and now trying to authenticate a .Net Core application. Even after configuring the Startup.cs, when I run the solution, the system is still navigating to /Account/Login; I am expecting the system to navigate to Identity Server.

Below is my Startup.cs code.

public void ConfigureServices(IServiceCollection services)
    {

        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultAuthenticateScheme = "oidc";
        }).AddCookie(options =>
        {
            options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
            options.Cookie.Name = "identitycookie";
        }).AddOpenIdConnect("oidc", options =>
            {
                options.Authority = "https://localhost:44123/identity";
                options.ClientId = "wp7jfcxEHaRE8DUIZka";
                options.ResponseType = "id_token token";
                options.SaveTokens = true;
                options.SignInScheme = "Cookies";
                options.Configuration = new OpenIdConnectConfiguration
                {
                    AuthorizationEndpoint =
                        "https://localhost:44123/identity/connect/authorize",
                    TokenEndpoint =
                        "https://localhost:44123/identity/connect/token"
                };
            });

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

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc();
    }

Could somebody let me know what I am doing wrong.

Thanks in advance

2 Answers

Use below code :

 options.DefaultChallengeScheme = "oidc";

Instead of :

options.DefaultAuthenticateScheme = "oidc";

That will challenge oidc scheme and make user redirect to an external authentication provider.

Solution by Nan Yu works well.

An alternative is to set CookieAuthenticationOptions.ForwardChallenge. This way, even if Challenge is triggered on default "Cookies" scheme, it will be forwarded to "oidc" scheme:

.AddCookie(options =>
    {
        // [...]
        options.ForwardChallenge = "oidc";
    })
Related