身份服务器 4 OidcClient 使用另一个应用程序生成的令牌

Identity server 4 OidcClient using token generated by another application

大家好,如果我要朝着正确的方向前进,我需要一些指导。 我有具有此设置的身份服务器客户端:

new Client
            {
                ClientId = "XamarinAndAngularClient",
                ClientName = "Xamarin and Angular client(Code with PKCE)",
                RedirectUris = new List<string>
                {
                "http://localhost:4200", 
                "http://localhost:4200/auth-callback" 
                },
                PostLogoutRedirectUris =
                {

                    "http://localhost:4200",
                    "https://localhost:4200",
                },
                RequireClientSecret = false,
                AllowedGrantTypes = GrantTypes.Code,
                RequirePkce = true,
                AllowedScopes =
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    IdentityServerConstants.StandardScopes.Address,
                    "myprotectedeapi"
                },

                AllowOfflineAccess = true,
                RefreshTokenUsage = TokenUsage.ReUse
            }

此客户端由 Angular SPA 与 oidc-client-js 使用,效果很好。我添加了 Xamarin 应用程序以使用具有以下设置的相同客户端:

var options = new OidcClientOptions
        {
            Authority = "https://myidentityserver.com/",
            ClientId = "XamarinAndAngularClient",
            Scope = "openid profile address myprotectedeapi",
            //RedirectUri = "xamarinformsclients://callback",
            RedirectUri = "http://localhost:4200/auth-callback",
            Browser = browser,
            FilterClaims =  false,
            Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
            ResponseMode = OidcClientOptions.AuthorizeResponseMode.Redirect, 
        };

Xamarin 表单设置的想法是将用户重定向到 Indentity 服务器登录屏幕,并在他提供所需凭据时重定向到 Angular SPA。当前,每次 oidc-client-js 返回错误时都会发生这种重定向到 Angular 的情况:在存储中找不到匹配的状态。我的问题是:我当前的 idea/approach 是否正确,如果不正确,我可以做些什么来 "inform" 代码(包含身份服务器生成的状态和会话状态)的 oidc-client-js来自 Xamarin 的回调是否正常并且可以使用?

这里的问题,正如图书馆告诉你的那样,是第一个请求到 authorize 端点的应用程序确实必须与获得 auth 回调的应用程序相同对 token 端点的后续请求。

code + PKCE 流程不需要使用客户端密钥,因为它保证执行第一个请求授权的同一客户端,执行第二个请求以令牌(这就是 PKCE 所做的)。这里唯一的秘密不是秘密,它是你的 redirect_uri 所以只有位于该 uri 上的应用程序才能完成流程。通过这种方式,我们无需密码即可验证 client(您的 angular SPA)。

话虽如此,您可以在 Xamarin.Forms 上做的是调用 SPA 上的端点,从那里启动整个流程,这在维护方面也更方便,因为您只需将 oidc 配置保留在应用程序的一侧 (angular spa)。

我在

上解释了有关此流程的更多细节