即使在实施IdentityServer之后,ASP.Net Core应用程序仍重定向到/ Account / Login

时间:2019-10-09 01:05:30

标签: asp.net-core openid-connect asp.net-core-2.2

我创建了一个简单的Identity Server,现在尝试对.Net Core应用程序进行身份验证。 即使配置了Startup.cs之后,当我运行解决方案时,系统仍导航到/ Account / Login; 我希望系统导航到Identity Server。

下面是我的Startup.cs代码。

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();
    }

有人能让我知道我做错了吗

预先感谢

2 个答案:

答案 0 :(得分:0)

使用以下代码:

 options.DefaultChallengeScheme = "oidc";

代替:

options.DefaultAuthenticateScheme = "oidc";

这将挑战oidc模式,并使用户重定向到外部身份验证提供程序。

答案 1 :(得分:0)

Solution by Nan Yu效果很好。

另一种方法是设置CookieAuthenticationOptions.ForwardChallenge。这样,即使在默认的“ Cookies”方案中触发了“挑战” ,也会将其转发到“ oidc”方案:

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