Ano*_*tor 9 openid-connect identityserver4 oidc-client-js
我在以下代码中得到用户未定义。
我已经从 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;
});
Run Code Online (Sandbox Code Playgroud)
在 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
}
}
Run Code Online (Sandbox Code Playgroud)
signinSilentCallback:返回从授权端点通知父窗口响应的承诺。 https://github.com/IdentityModel/oidc-client-js/wiki
signinSilentCallback - 这不是会返回用户对象的东西。
如果您确实需要在静默更新时获取用户对象,我建议您将这种方法与以下代码片段一起使用。这也适用于我的 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);
});
});
});
Run Code Online (Sandbox Code Playgroud)
由于 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
});
Run Code Online (Sandbox Code Playgroud)
并且您可以使用 UserManager 事件在刷新令牌时加载新用户
this.mgr.events.addUserLoaded(args => {
this.mgr.getUser().then(user => {
this._user = user; // load the new user
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8462 次 |
| 最近记录: |