我在登录身份服务器后遇到重定向问题。
我有以下角度验证oidc客户端配置:
export function configureAuth(oidcConfigService: OidcConfigService) {
return () =>
oidcConfigService.withConfig({
stsServer: 'http://localhost:5002',
redirectUrl: window.location.origin,
postLogoutRedirectUri: window.location.origin,
clientId: 'applications-portal',
scope: 'openid profile',
responseType: 'id_token token',
logLevel: LogLevel.Debug,
});
}
和app.component.ts:
ngOnInit() {
this.oidcSecurityService.checkAuth().subscribe((auth) => {
console.log('is authenticated', auth);
if (!auth) {
this.login();
}
});
}
login() {
this.oidcSecurityService.authorize();
}
这是身份服务器应用程序中的客户端配置:
new Client
{
ClientId = "applications-portal",
ClientName = "Applications Portal",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedScopes =
{
"service",
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
},
AccessTokenType = AccessTokenType.Jwt,
AllowAccessTokensViaBrowser = true,
RequireConsent = false,
RequireClientSecret = false,
RequirePkce = true,
RedirectUris = {
"http://localhost:4200",
},
PostLogoutRedirectUris =
{
"http://localhost:4200"
},
AllowedCorsOrigins =
{
"http://localhost:4200"
},
}
和StartUp.cs:
services.ConfigureApplicationCookie(config =>
{
config.Cookie.Name = "Identity.Cookie";
config.LoginPath = "/Auth/Login";
});
问题是,当我在Login(get(方法中重定向到AuthController时,我得到的returnUrl如下所示:returnUrl值
登录后,它不会将我重定向回客户端应用程序,而是停留在登录页面上。我相信returnUrl本身有问题。我是第一次使用IdentityServer,所以我真的不知道该挖掘什么。
更新:
问题出在Chrome浏览器中。SameSite的东西阻止它重定向。我在这里尝试过解决方案https://www.thinktecture.com/en/identity/samesite/prepare-your-identityserver/但没有奏效。在其他浏览器中,它可以正常工作。你能给我一个提示吗?在这种情况下,使用Chrome该怎么办?
我也试过将其设置为Lax,但没有任何变化。
services.ConfigureApplicationCookie(config =>
{
config.Cookie.Name = "Identity.Cookie";
config.LoginPath = "/Auth/Login";
config.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax;
});
一个想法是重定向URL是否应该是:
RedirectUris = {"http://localhost:4200/auth-callback"}.
链接应该指向Angular希望将令牌发送到的URL。
请参阅以下链接:
- IdentityServer外部身份验证提供程序-身份验证回调-重定向-400错误请求
- Angular、Asp.Net Core和Identity Server的用户身份验证
通过将Cookie配置更改为:来解决此问题
services.ConfigureApplicationCookie(config =>
{
config.Cookie.Name = "Identity.Cookie";
config.LoginPath = "/Auth/Login";
config.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax;
});
在配置方法中:
app.UseCookiePolicy(new CookiePolicyOptions
{
MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.Lax,
});