ASPNET Core OIDC 关联失败

2024-05-18

我在 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"]));

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

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

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

app.Use((context, next) =>
{
    context.Request.Scheme = "https";
    return next();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASPNET Core OIDC 关联失败 的相关文章

随机推荐