【问题标题】:Thinktecture IdentityServer v3 LogOut for Implicit flowThinktecture IdentityServer v3 LogOut 用于隐式流
【发布时间】:2015-11-25 16:30:19
【问题描述】:

如何获取隐式令牌的 id_token 以传递 id_token 提示以注销隐式流或有其他方法?我有端点/connect/endsession? id_token_hint=

不确定如何从隐含流中获取 id_token,我得到的只是 access_token 和过期时间。 IdSvr 中有设置吗?

【问题讨论】:

    标签: thinktecture-ident-server


    【解决方案1】:

    这包含三个组成部分。

    当您在 Startup.cs 中配置 OIDC 身份验证时,首先确保您从 Identity Server 请求 id_token(如上面的 @leastprivilege 所述):

    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    {
         Authority = "https://localhost:44301/",
         ...
         ResponseType = "id_token token", //(Here's where we request id_token!)
    

    其次,使用 OIDC 通知并在验证安全令牌后,将 id_token 添加到用户的声明中:

    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        SecurityTokenValidated = async n =>
                        {
    
                            var nid = new ClaimsIdentity(
                                n.AuthenticationTicket.Identity.AuthenticationType,
                                Constants.ClaimTypes.GivenName,
                                Constants.ClaimTypes.Role);
    
                            // get userinfo data
                            var userInfoClient = new UserInfoClient(
                                new Uri(n.Options.Authority + "/" + Constants.RoutePaths.Oidc.UserInfo),
                                n.ProtocolMessage.AccessToken);
    
                            var userInfo = await userInfoClient.GetAsync();
                            userInfo.Claims.ToList().ForEach(ui => nid.AddClaim(new Claim(ui.Item1, ui.Item2)));
    
                            // keep the id_token for logout (**This bit**)
                            nid.AddClaim(new Claim(Constants.TokenTypes.IdentityToken, n.ProtocolMessage.IdToken));
    
                            n.AuthenticationTicket = new AuthenticationTicket(
                                nid,
                                n.AuthenticationTicket.Properties);
                    },
    

    最后,在重定向注销(也是一个通知事件)时,您将 id_token 添加到协议消息中:

                RedirectToIdentityProvider = n =>
                {
                    if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
                    {
                        var idTokenHint = n.OwinContext.Authentication.User.FindFirst(Constants.TokenTypes.IdentityToken);
    
                        if (idTokenHint != null)
                        {
                            n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
                        }
                    }
    
                    return Task.FromResult(0);
                }
    

    您还需要确保在 Identity Server 中的客户端上设置 PostLogoutRedirectUris:

        new Client
        {
            Enabled = true,
            ClientName = "(MVC) Web App",
            ClientId = "mvc",
            Flow = Flows.Implicit,
            PostLogoutRedirectUris = new List<string>
            {
                "https://localhost:44300/" //(** The client's Url**)
            }
         }
    

    这将确保您在用户注销时可以选择返回到授权客户端 :)

    所有这些都与 https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html 的 MVC 示例非常相似

    比你要求的要多一点,但希望这也能帮助其他想弄清楚的人:)

    【讨论】:

    • Identity Server 4 有什么不同吗?如果是,我将创建一个新的 SO 问题,询问应如何在 IdSrv4 中实现单次注销。
    • @JonasAxelsson - 我不知道。当我重新访问堆栈的这一部分时,我会尝试更新这些问题。
    【解决方案2】:

    要获得 id_token,您必须提出要求。使用response_type=id_token token

    【讨论】:

      【解决方案3】:

      你试过了吗?

      ASP.Net Identity Logout

      它应该自动创建 id 令牌提示

      【讨论】:

        猜你喜欢
        • 2015-05-02
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 2015-03-18
        • 2015-04-20
        • 1970-01-01
        • 2016-05-03
        • 2015-06-21
        相关资源
        最近更新 更多