asp.net - PostLogoutRedirectUri 在带有 SPA 的身份服务器 4 中始终为空(Angular 7 OIDC 客户端)
问题描述
在具有身份服务器的 Angular 应用程序中使用 facebook 登录身份验证 4。在注销方法PostLogoutRedirectUri ,ClientName,LogoutId始终为空。
private async Task<LoggedOutViewModel> BuildLoggedOutViewModelAsync(string logoutId)
{
// get context information (client name, post logout redirect URI and iframe for federated signout)
var logout = await _interaction.GetLogoutContextAsync(logoutId);
var vm = new LoggedOutViewModel
{
AutomaticRedirectAfterSignOut = AccountOptions.AutomaticRedirectAfterSignOut,
PostLogoutRedirectUri = logout?.PostLogoutRedirectUri,
ClientName = string.IsNullOrEmpty(logout?.ClientName) ? logout?.ClientId : logout?.ClientName,
SignOutIframeUrl = logout?.SignOutIFrameUrl,
LogoutId = logoutId
};
if (User?.Identity.IsAuthenticated == true)
{
var idp = User.FindFirst(JwtClaimTypes.IdentityProvider)?.Value;
if (idp != null && idp != IdentityServer4.IdentityServerConstants.LocalIdentityProvider)
{
var providerSupportsSignout = await HttpContext.GetSchemeSupportsSignOutAsync(idp);
if (providerSupportsSignout)
{
if (vm.LogoutId == null)
{
// if there's no current logout context, we need to create one
// this captures necessary info from the current logged in user
// before we signout and redirect away to the external IdP for signout
vm.LogoutId = await _interaction.CreateLogoutContextAsync();
}
vm.ExternalAuthenticationScheme = idp;
}
}
}
return vm;
}
Angular oidc 客户端代码
logout(): Promise<any> {
return this._userManager.signoutRedirect();
}
客户端设置
public IEnumerable<Client> GetClients()
{
var client = new List<Client>
{
new Client
{
ClientId = ConstantValue.ClientId,
ClientName = ConstantValue.ClientName,
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RequireConsent = false,
RedirectUris = { string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "assets/oidc-login-redirect.html"), string.Format("{0}/{1}", Configuration["IdentityServerUrls:ClientUrl"], "assets/silent-redirect.html") },
PostLogoutRedirectUris = { string.Format("{0}?{1}", Configuration["IdentityServerUrls:ClientUrl"] , "postLogout=true") },
AllowedCorsOrigins = { Configuration["IdentityServerUrls: ClientUrl"] },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
ConstantValue.ClientDashApi
},
IdentityTokenLifetime=120,
AccessTokenLifetime=120
},
};
return client;
}
logoutId 始终为空。我能够成功登录到 facebook 返回回调方法。但重定向 uri 始终为空。
解决方案
这可能不是您的问题,但是当我遇到与您相同的错误时,这是我的问题,因此我在这里发布自己的经验。
我正在关注一个 Pluralsight 视频,该视频正在使用 IdentityServer4 作为 STS 服务器构建一个 Angular 应用程序,它指示我在我正在构建的 AuthService 中的 UserManager 的配置中设置post_logout_redirect_uri ,如下所示:
var config = {
authority: 'http://localhost:4242/',
client_id: 'spa-client',
redirect_uri: `${Constants.clientRoot}assets/oidc-login-redirect.html`,
scope: 'openid projects-api profile',
response_type: 'id_token token',
post_logout_redirect_uri: `${Constants.clientRoot}`,
userStore: new WebStorageStateStore({ store: window.localStorage })
}
this._userManager = new UserManager(config);
github repo https://github.com/IdentityServer/IdentityServer4/issues/396上的一个老问题讨论了这样一个事实,即这是现在自动设置的,不需要显式设置(参见线程的结尾)。一旦我从配置中删除它,我就不再遇到 AccountController 的 Logout 方法中logoutId为空的问题:
/// <summary>
/// Show logout page
/// </summary>
[HttpGet]
public async Task<IActionResult> Logout(string logoutId)
所以这对我来说是正确的配置设置:
var config = {
authority: 'http://localhost:4242/',
client_id: 'spa-client',
redirect_uri: `${Constants.clientRoot}assets/oidc-login-redirect.html`,
scope: 'openid projects-api profile',
response_type: 'id_token token',
userStore: new WebStorageStateStore({ store: window.localStorage })
}
this._userManager = new UserManager(config);
推荐阅读
- c++ - 有人可以用 bool 函数和 if 语句来解释这种行为吗?
- amazon-web-services - 多租户动态多域,SSL 指向单个主机/CDN
- python - 如何解决leetcode中超出时间限制的错误
- java - 如何从java中的链接列表中的节点检索对象的内容
- google-cloud-platform - 使用 XCom 通过 GoogleCloudStorageToBigQueryOperator 在 Airflow 中加载 Schema
- android - Android:如何制作特殊的recyclerview布局
- observable - 当我尝试编译时,我不断收到这个错误类不是抽象的,并且没有覆盖 MyObserver 中的抽象方法 update(Object)
- strftime - 如何编写符合 Flake8 的 strftime 表达式?
- javascript - 如何将 Google Analytics api 响应放入工作表中?
- aws-amplify - 放大 @model 语法以仅生成创建和更新突变(而不是删除)