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

时间:2017-01-03 18:33:52

标签: c# asp.net jwt

我的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
}

我想在令牌有效负载中添加一些自定义字段/属性,例如ProfilePicURL,以便有效负载看起来像这样:

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

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

1 个答案:

答案 0 :(得分:13)

poll公开JwtPayload Payload { get; set;}属性。 JwtPayload派生自{-# LANGUAGE FlexibleContexts #-} module Network.AMQP.Worker.Poll where import Control.Concurrent (threadDelay) import Control.Monad.Trans.Control (MonadBaseControl) import Control.Monad.Base (liftBase) import Control.Monad.Loops (untilJust) poll :: (MonadBaseControl IO m) => Int -> m (Maybe a) -> m a poll us action = untilJust $ do ma <- action case ma of Just a -> return $ Just a Nothing -> do liftBase $ threadDelay us return Nothing ,因此只需将其添加到有效负载中......

JwtSecurityToken