c# - 如何从经过身份验证的 SecurityToken 中获取声明

标签 c# jwt identityserver3

我将一个 token 作为字符串传递到 SOAP 服务中,并验证了该 token 是有效的。我现在有一个 SecurityToken,在 Debug模式下我可以看到所有声明,特别是我想传递给另一个方法的 userId 声明。我似乎无法弄清楚如何获得 claim 。现在我解码了 token 的字符串版本( token 的未经验证的字符串版本,我至少等到成功验证之后。)这是该代码块:

SecurityToken validatedToken = null;

if (VerifyToken(sPassword, ref response, ref validatedToken))
{
    var claimsObj = JObject.Parse(Encoding.UTF8.GetString(Base64Url.Decode(claims)));
    JToken userId = claimsObj.GetValue("userId");
    return implClass.Process(userId);
}

如何从 SecurityToken 中获取声明?

validatedToken.Id; // not it
validatedToken.ToClaimsPrincipal(cert); // doesn't work

就公开的属性而言,我似乎没有其他任何希望,但由于我可以在调试器中看到声明,我确信有一种方法我只是没有看到。

最佳答案

我刚刚遇到了同样的事情,即在调试时,Payload 属性在 SecurityToken 上可见,但在编辑代码时似乎没有 Payload 属性。

看起来 SecurityToken 的底层类型是 JwtSecurityToken(尽管 securityCode.GetType() 的 QuickWatch 返回的是 SecurityToken,而不是 JwtSecurityToken。)。不管怎样,转换为实际的底层类型并引用 Payload 集合对我有用。

一个解决方案:

string userId = ((JwtSecurityToken)access_token).Payload["userId"].ToString();

关于c# - 如何从经过身份验证的 SecurityToken 中获取声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174039/

相关文章:

安卓刷新 token

asp.net-core - uri 查询参数中的 asp.net core JWT?

c# - 通过 UseOpenIdConnectAuthentication 登录时,loginInfo 始终为 null

c# - 使用表达式查询 Azure 表存储

c# - while/for 循环直到出现特定条件

c# - 使用字符串值通过反射设置属性

c# - 在 Microsoft.Owin.Security.OpenIdConnect 中间件的响应中添加位置 header

c# - Unity Raycast 忽略 LayerMask

python - 是否可以使用签名的 JWT 对 Google Drive API 进行授权调用?

c# - OWIN Google 身份验证 - 如何获取 id_token?