首页 > 解决方案 > ASPNET Core OIDC 关联失败

问题描述

我在 StackOverflow 上查看了与此类似的一堆类似问题,但没有一个解决方案对我有用。

这个问题快把我逼疯了!

我与这里的许多类似服务器的主要区别是负载均衡器后面只有一台服务器,所以问题不在于我的请求会发送到不同的服务器。我已经实现了数据保护中间件,更改了我的回调路径,尝试捕获错误事件,为状态数据添加了缓存等。没有什么可以解决这个问题。我不知道我错过了什么。

如果有人能给我一些关于这方面的见解,我将不胜感激。

services.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";
                options.ClientId = Configuration["IdServerClientId"];
                options.ClientSecret = Configuration["IdServerClientSecret"];
                options.Authority = Configuration["IdServerBaseUri"];
                options.CallbackPath = "/sign-in-oidc2";
                options.RequireHttpsMetadata = false;
                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    NameClaimType = "name"
                };
                options.Scope.Add("openid");
                options.Scope.Add("profile");
                options.Scope.Add("email");
                options.Events = new OpenIdConnectEvents()
                {
                    //OnRedirectToIdentityProvider = OnRedirectToProvider,
                    OnRemoteFailure = OnRemoteFailure,
                    OnAuthenticationFailed = OnAuthenticationFailed
                };
            });

        services.AddOidcStateDataFormatterCache("foo");

        services.AddDataProtection()
            .PersistKeysToFileSystem(new DirectoryInfo(Configuration["KeyPersistenceLocation"]));

标签: asp.net-core-2.0identityserver4openid-connect

解决方案


如果其他人遇到此问题,请在此处找到答案:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1#when-it-isnt-possible-to-add-forwarded-标头和所有请求都是安全的

在 Startup 的 Configure 方法中,需要添加这个来处理 http/https 冲突。

app.Use((context, next) =>
{
    context.Request.Scheme = "https";
    return next();
});

推荐阅读