signinSilentCallback 中没有用户使用身份服务器和 javascript 的 oidc 客户端

No user in signinSilentCallback using identityserver and oidc client of javascript

我在下面的代码中得到了未定义的用户。

我已经通过 MVC 验证了用户。

但是当我使用 signinSilentCallback 获取该用户的详细信息时,它在 js 中使用 oidc-client 变得不确定。

它也没有给出任何错误。

        var mgr = new UserManager({
                    authority: "http://localhost:5000",
                    client_id: "js",
                    redirect_uri: "http://localhost:50144/signin-oidc",
                    silent_redirect_uri: "http://localhost:50144/signin-oidc",
                    response_type: "id_token token",
                    post_logout_redirect_uri: "http://localhost:50144/signout-callback-oidc",
                });

        mgr.signinSilentCallback().then(function (user) {

            //**Here user is undefined.**
            axios.defaults.headers.common['Authorization'] = "Bearer " + user.access_token;

        });

在 Identityserver 4 中,客户端定义如下。

new Client
                {
                    ClientId = "js",
                    ClientName = "js",
                    ClientUri = "http://localhost:50144",

                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowAccessTokensViaBrowser = true,
                    RequireClientSecret = false,
                    AccessTokenType = AccessTokenType.Jwt,

                    RedirectUris = 
                    {
                        "http://localhost:50144/signin-oidc",
                    },

                    PostLogoutRedirectUris = { "http://localhost:50144/signout-callback-oidc" },
                    AllowedCorsOrigins = { "http://localhost:50144" },

                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.Email
                    }
                }

signinSilentCallback: Returns promise to notify the parent window of response from the authorization endpoint. https://github.com/IdentityModel/oidc-client-js/wiki

signinSilentCallback - 这不是 return 你的用户对象。

如果您确实需要在静默更新中获取用户对象,我建议将此方法与以下代码片段一起使用。这在 salesforce 应用程序中也适用于我。

this.userManager.events.addAccessTokenExpiring(() =>
            {
                this.userManager.signinSilent({scope: oidcSettings.scope, response_type: oidcSettings.response_type})
                    .then((user: CoreApi.Authentication.Interfaces.OidcClientUser) =>
                    {
                        this.handleUser(user); // This function just set the current user
                    })
                    .catch((error: Error) =>
                    {
                        this.userManager.getUser()
                            .then((user: CoreApi.Authentication.Interfaces.OidcClientUser) =>
                            {
                                this.handleUser(user);
                            });
                    });
            });

由于在 oidc-client js 中为 iFrame 报告的错误之一,我们还需要处理 catch 中的 getUser

从上面的代码着重于令牌过期时执行静默更新的方式。

您可以在配置中将 automaticSilentRenew 设置为 true

var mgr = new UserManager({
                authority: "http://localhost:5000",
                client_id: "js",
                redirect_uri: "http://localhost:50144/signin-oidc",
                silent_redirect_uri: "http://localhost:50144/signin-oidc",
                response_type: "id_token token",
                post_logout_redirect_uri: "http://localhost:50144/signout-callback-oidc",
                automaticSilentRenew: true; //here

            });

并且您可以使用 UserManager 事件在刷新令牌时加载新用户

this.mgr.events.addUserLoaded(args => {
  this.mgr.getUser().then(user => {
    this._user = user; // load the new user
  });
});