C# System.IdentityModel.Tokens.JwtSecurityToken自定义属性

C# System.IdentityModel.Tokens.JwtSecurityToken自定义属性,c#,asp.net,jwt,C#,Asp.net,Jwt,My AuthServer当前正在使用以下代码生成JwtSecurityToken: var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey); var handler = new JwtSecurityTokenHandler(); var jwt = handler.Wr

My AuthServer当前正在使用以下代码生成JwtSecurityToken:

var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
var handler = new JwtSecurityTokenHandler();
var jwt = handler.WriteToken(token);
有效载荷如下所示:

{
  "unique_name": "myUserName",
  "sub": "myUserName",
  "role": "API_User",
  "iss": "Automation",
  "aud": "099153c2625149bc8ecb3e85e03f0022",
  "exp": 1486056731,
  "nbf": 1483464731
}
{
  "unique_name": "myUserName",
  "sub": "myUserName",
  "role": "API_User",
  "iss": "Automation",
  "aud": "099153c2625149bc8ecb3e85e03f0022",
  "exp": 1486056731,
  "nbf": 1483464731,
  "profilePicture": "http://url/user.jpg"
}
我想在令牌负载中添加一些自定义字段/属性,例如ProfilePicURL,以便负载看起来像这样:

{
  "unique_name": "myUserName",
  "sub": "myUserName",
  "role": "API_User",
  "iss": "Automation",
  "aud": "099153c2625149bc8ecb3e85e03f0022",
  "exp": 1486056731,
  "nbf": 1483464731
}
{
  "unique_name": "myUserName",
  "sub": "myUserName",
  "role": "API_User",
  "iss": "Automation",
  "aud": "099153c2625149bc8ecb3e85e03f0022",
  "exp": 1486056731,
  "nbf": 1483464731,
  "profilePicture": "http://url/user.jpg"
}

如何添加这些自定义属性并确保令牌包含它们?

JwtSecurityToken
公开属性。派生自
字典
,因此只需将其添加到有效负载中即可

var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
token.Payload["profilePicture"] = "http://url/user.jpg"
var handler = new JwtSecurityTokenHandler();
var jwt = handler.WriteToken(token);

使用
WriteToken
对令牌进行编码和签名是很重要的,因为简单地获取
RawData
属性将不起作用(令牌将不包含自定义声明)。

将其添加到有效负载属性,该属性派生自
Dictionary
ie
token.Payload[“profilePicture]”http://url/user.jpg"
谢谢!!在任何地方都找不到,不知道这是一本可以编辑的字典!