登录身份服务器后无法重定向回 angular 客户端
Cannot redirect back to angular client after login in identity server
我在使用身份服务器登录后遇到重定向问题。
我有以下 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 做什么吗?
我也试过将其设置为 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"}.
link 应该是 URL,Angular 希望将令牌发送到 URL。
查看这些 link:
通过将 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,
});
我在使用身份服务器登录后遇到重定向问题。
我有以下 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 做什么吗?
我也试过将其设置为 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"}.
link 应该是 URL,Angular 希望将令牌发送到 URL。
查看这些 link:
通过将 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,
});