claims-based-identity - 添加 id_token 作为声明 AspNetCore OpenIdConnect 中间件

标签 claims-based-identity openid-connect identityserver3 .net-core-rc2

我正在尝试设置 IdTokenHint发送注销请求时。上一篇 Microsoft.Owin.Security.OpenIdConnect中间件我可以设置 id_token作为 SecurityTokenValidated 中的 claim 使用 SecurityTokenValidated 的方法通过执行以下操作来通知:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    ...
    Notifications = new OpenIdConnectAuthenticationNotifications
    {
        //Perform claims transformation
        SecurityTokenValidated = async notification =>
        {
            ...
            notification.AuthenticationTicket.Identity.AddClaim(new Claim("id_token", notification.ProtocolMessage.IdToken));
        },
        RedirectToIdentityProvider = async n =>
        {
            if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
            {
                var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
                n.ProtocolMessage.IdTokenHint = idTokenHint;
             }
         }
    }
}

使用新的中间件 Microsoft.AspNetCore.Authentication.OpenIdConnect (在 ASP.NET Core RC2 中)我在尝试完成同样的事情时遇到了麻烦。我假设我应该使用 Events像这样。
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
    ...
    Events = new OpenIdConnectEvents()
    {
         OnTokenValidated = context =>
         {
             ...
             context.SecurityToken.Payload.AddClaim(new Claim("id_token", context.ProtocolMessage.IdToken));
          },
          OnRedirectToIdentityProviderForSignOut = context =>
          {
                var idTokenHint = context.HttpContext.User.FindFirst("id_token").Value;
                context.ProtocolMessage.IdTokenHint = idTokenHint;
        }
     }
 }

我看到的问题是声明没有保留在 SecurityToken 上。并且不要在 HttpContext.User 上设置.我错过了什么?

最佳答案

关于上面的代码,至少在 ASP.NET Core 2.1 版中,可以通过 context.Properties.GetTokenValue(...) 访问 ID token 。 (而不是作为用户声明)。
然而,正如 Brock Allen 在 comment to your question 中所说的那样, OpenIdConnectHandler将自动包含 idTokenHint登出时。然而,今天这让我咬了几个小时,除非 OpenIdConnectOptions.SaveTokens,否则它不会保存 token 以备后用。设置为 true .默认为 false .
所以,如果 SaveTokenstrue ,处理程序将自动包含 idTokenHint ,您可以手动访问 id token通过 context.Properties.GetTokenValue(...) .

关于claims-based-identity - 添加 id_token 作为声明 AspNetCore OpenIdConnect 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576666/

相关文章:

sharepoint - Visual Studio 部署未激活 SharePoint 功能

asp.net-core - Asp.net Core Identity 使用 AspNetUserClaims 还是 AspNetRoleClaims?

amazon-cognito - Amplify 缺少 Cognito 自定义声明,但 Appsync 控制台没有

asp.net-core - 存储在 Cookie 中的声明超过最大请求 header 长度

graphql - 如何在 Graphql 架构级别检查 AWS AppSync OIDC 指令的角色或权限

asp.net-identity - 如何向 IdentityServerServiceFactory 注册 UserManager?

single-sign-on - 同一域中的 OpenID Connect RP 和 OP?

flutter - 无法使用 openid_client 通过带有 keycloak 的 pkce flutter 应用程序进行身份验证

extend - 我可以向基于 Identity Server 3 的服务添加服务信息/健康检查端点吗?

asp.net - 使用 Identity Server 3,ClaimsPrinciple 即使在不记名 token 身份验证成功后仍为空