如何解决登录身份服务器后无法重定向回角度客户端
在使用身份服务器登录后,重定向存在问题。
我有以下angular-auth-oidc-client配置:
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";
});
问题是当我在登录(GET)方法中重定向到AuthController时,得到的returnUrl如下所示: returnUrl Value
登录后,它不会将我重定向回客户端应用程序,而是停留在登录页面上。我相信returnUrl本身有问题。我是第一次使用IdentityServer,所以我真的不知道要做什么。
已更新:
问题出在Chrome浏览器中。 SameSite阻止它重定向。我在这里https://www.thinktecture.com/en/identity/samesite/prepare-your-identityserver/尝试过解决方案 但这没用。在其他浏览器中,它可以按预期工作。您能否给我一个提示,在这种情况下如何使用Chrome?
我还尝试将其设置为“宽松”,但没有任何变化。
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 External auth provider - auth-callback - Redirection - 400 Bad request
- User Authentication and Identity with Angular,Asp.Net Core and IdentityServer
通过将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,});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。