首页 > 解决方案 > Identityserver4 使用两个相同的外部提供者,第二个外部提供者登录总是失败

问题描述

我使用两个具有不同 clientid 的不和谐提供程序进行登录。以下代码:

services.AddAuthentication()
                .AddDiscord("ADiscord", u =>
                {
                    u.ClientId = "74627xxx8536";
                    u.ClientSecret = "B-FLxxxxjp3JOKwr27";
                    u.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    u.Scope.Add("guilds.join");
                    u.SaveTokens = true;
                    u.CorrelationCookie.SameSite = SameSiteMode.Unspecified;
                    u.CorrelationCookie.IsEssential = true;
                })
                .AddDiscord("BDiscord", u =>
                {
                    u.ClientId = "71475xxx1925";
                    u.ClientSecret = "45-xxxA4okXx1I";
                    u.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    u.Scope.Add("guilds.join");
                    u.SaveTokens = true;
                    u.CorrelationCookie.SameSite = SameSiteMode.Unspecified;
                    u.CorrelationCookie.IsEssential = true;
                });

当我使用方案“ADiscord”登录时,一切正常。但是使用方案“BDiscord”它会抛出错误“来自 RemoteAuthentication 的错误:oauth 状态丢失或无效”。更奇怪的是,如果更改顺序让“BDiscord”在顶部然后使用方案“BDiscord”登录一切正常,“ADiscord”会抛出错误。

Disord 提供程序代码位于https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/tree/dev/src/AspNet.Security.OAuth.Discord

标签: identityserver4discord.net

解决方案


您需要设置CallbackPath,将其设置为 sth like/signin-discord/signin-discord使用 IdSvr4 注册。此端点用于在重定向到应用程序端点之前完成 OAuth 握手。


推荐阅读