【问题标题】:How to retrieve an OpenID Connect Identity Token from a cookie如何从 cookie 中检索 OpenID Connect 身份令牌
【发布时间】:2015-09-03 08:52:56
【问题描述】:

如何从 Microsoft 的基于 OWIN 的中间件生成的 cookie 中检索 OpenID 连接令牌?

我正在使用 Microsoft.Owin.Security.CookiesMicrosoft.Owin.Security.OpenIdConnect 来保护使用“隐式流”的网站。有时我认为我可能能够更好地理解事物或能够排除故障,我可以检查“原始”令牌而不是从中生成的对象模型。

我了解信息是通过 Cookie 存储的,但尚未找到如何从 cookie 中检索令牌。这是一个开发环境,因此我应该可以访问所需的任何证书/机密。

我了解令牌应该有 3 个以句点分隔的段:{header}.{claims}.{signature}。如果我能找到我了解到的令牌,我可以使用jwt.io 来查看内容。但是,我的 cookie 中没有一个内容与该格式匹配。

这是我正在使用的中间件配置:

app.SetDefaultSignInAsAuthenticationType( CookieAuthenticationDefaults.AuthenticationType );
app.UseCookieAuthentication( new CookieAuthenticationOptions() );

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
        {
        ClientId = clientId,
        Authority = stsAuthority,
        RedirectUri = baseHostingPath,
        ResponseType = "id_token",
        Scope = string.Join( " ", "openid", "profile", "email" )
        } );

【问题讨论】:

    标签: c# authentication cookies owin openid-connect


    【解决方案1】:

    如果您只想在调试时执行此操作,我建议您尝试https://github.com/vibronet/OInspector/tree/dev - 它可以帮助您检查 Fiddler 中的令牌。

    如果您想在代码中执行此操作,您可以确保原始令牌保存在 ClaimsPrincipal 中

    • 添加

      TokenValidationParameters = new TokenValidationParameters
      { 
          SaveSigninToken = true 
      }
      

      到选项初始化

    • 通过某种方式检索令牌

      var ci = (System.Security.Claims.ClaimsIdentity)
                   ClaimsPrincipal.Current.Identity;
      string token = ((System.IdentityModel.Tokens.BootstrapContext)
                         ci.BootstrapContext).Token;
      

    【讨论】:

      猜你喜欢
      • 2018-11-16
      • 2021-03-04
      • 2022-01-06
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 2015-12-25
      • 2014-09-12
      • 1970-01-01
      相关资源
      最近更新 更多