我正在使用 oidc-client 和 Angular 来登录我的身份服务器。在同一台机器上运行这两个应用程序时,一切正常。
但是,当我在 Web 应用程序服务器上运行我的身份服务器并在静态 Web 应用程序上运行我的 Angular 应用程序时,我收到一个错误“invalid_grant”。
问题描述
用户单击登录按钮并被重定向到身份服务器的登录屏幕
用户输入密码和用户名,然后单击登录
身份服务器重定向到“登录回调”链接,我得到空白登录回调页面,因为登录回调只是一个 .ts 脚本
This URL appears in the header: https://white-wave-0ca3e8803.azurestaticapps.net/signin-callback?code=l5-p3B5IGifAZ3ijIUgKmLl7aL2g45Fh5JLmluOj3NA&scope=openid%20profile&state=6503e1f5f176411a890ac50a4d0ecb84&session_state=aPnjQ5ZUiMCDKkAztm0O0K4quSkfSlWRZd--YuK73Oc.NVxBA5K6RH5_ptpDobxmxA
在网络下,我们有一个红色的“令牌”错误。包含响应头
HTTP/1.1 400 错误请求
缓存控制:无存储,无缓存,最大年龄 = 0
编译指示:无缓存
内容长度:25
内容类型:应用程序/json;charset=UTF-8
服务器:Microsoft-IIS/10.0
Strict-Transport-Security:max-age=2592000
Access-Control-Allow-Origin:https : //white-wave-0ca3e8803.azurestaticapps.net/dashboard X-Powered-作者:ASP.NET
设置 Cookie:ARRAffinity=1fa22e5e18dd42b9d22d6c2c620b562f117da68736f4d32b741538a2d054d95c;Path=/;HttpOnly;Secure;Domain=hansecoreidp-staging.azurewebsites.net
设置 Cookie:ARRAffinitySameSite=1fa22e5e18dd42b9d22d6c2c620b562f117da68736f4d32b741538a2d054d95c;Path=/;HttpOnly;SameSite=None;Secure;Domain=hansecoreidp-staging.azurewebsites.net
日期:周四,2021 年 3 月 18 日 8 月 34 日格林威治标准时间:错误消息
{"error":"invalid_grant"}当我刷新时,我收到错误错误:未捕获(承诺中):错误:在本地存储、会话存储和 cookie 的存储中未找到匹配状态为空。
当我删除我的基本 URL https://white-wave-0ca3e8803.azurestaticapps.net之后的所有内容时,导航到基本 URL 并再次单击登录,登录有效并且在存储中我突然有了 Cookie Storage A cookie包含来自身份服务器会话存储的 AAR Affinity 等 我有access_token、id_token、配置文件、范围等。 本地存储 权限、client_id 等。
假设
我认为 Angular 应用程序无法将来自登录回调的信息存储在应用程序存储中。
身份服务器配置代码
public static IEnumerable<Client> StagingClients (IConfiguration configuration) =>
new Client[]
{
new Client
{
ClientName = "HAnsecore.Web",
ClientId = "clientid",
AllowedGrantTypes = GrantTypes.Code,
AccessTokenType = AccessTokenType.Jwt,
RedirectUris = new List<string>{ "https://white-wave-0ca3e8803.azurestaticapps.net/signin-callback" },
RequirePkce = true,
AllowAccessTokensViaBrowser = true,
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"hansecoreCQRSApi",
IdentityServerConstants.StandardScopes.OfflineAccess
},
AllowedCorsOrigins = { "https://white-wave-0ca3e8803.azurestaticapps.net" },
AllowOfflineAccess = true,
RequireClientSecret = false,
PostLogoutRedirectUris = new List<string> { "https://white-wave-0ca3e8803.azurestaticapps.net/signout-callback" },
RequireConsent = false,
AccessTokenLifetime = 600
}
};
角度 OIDC 客户端身份验证配置代码
private get idpSettings() : UserManagerSettings {
return {
authority: environment.idpAuthority,
client_id: environment.clientId,
redirect_uri: `${environment.clientRoot}/signin-callback`,
scope: "openid profile hansecoreCQRSApi offline_access",
response_type: "code",
post_logout_redirect_uri: `${environment.clientRoot}/signout-callback`
}
}
登录重定向回调代码 用户管理器是从oidc-client继承的类。调试代码时没有输入 .then ,我无法在此处进一步调试。但我认为这之后发生的任何事情都是代码失败的地方。
public finishLogin = (): Promise<User> => {
return this._userManager.signinRedirectCallback()
.then(user => {
this._loginChangedSubject.next(this.checkUser(user));
return user;
})
}
登录回拨码
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../../../core/services/auth.service';
@Component({
selector: 'app-signin-redirect-callback',
template: `<div></div>`
})
export class SigninRedirectCallbackComponent implements OnInit {
constructor(private _authService: AuthService, private _router: Router) { }
ngOnInit(): void {
this._authService.finishLogin()
.then(_ => {
this._router.navigate(['/'], { replaceUrl: true });
})
}
}
如果我的问题中缺少任何内容,请告诉我。
环境文件
export const environment = {
environmentName: 'STAGE',
production: false,
apiRoot: 'https://hansecoreapi-staging.azurewebsites.net/api/',
clientRoot: 'https://white-wave-0ca3e8803.azurestaticapps.net',
idpAuthority: 'https://hansecoreidp-staging.azurewebsites.net',
clientId: '54212148-15C1-40F9-BA07-E3CE748A7C7F'
};
