2
我使用JwtSecurityTokenHandler创建一个JWT令牌JwtSecurityTokenHandler WriteToken和“JSON序列化紧凑格式字符串”。
#region Assembly System.IdentityModel.Tokens.Jwt.dll, v4.0.20622.1351
\packages\System.IdentityModel.Tokens.Jwt.4.0.2.206221351\lib\net45\System.IdentityModel.Tokens.Jwt.dll
namespace System.IdentityModel.Tokens
{
// Summary:
// A System.IdentityModel.Tokens.SecurityTokenHandler designed for creating
// and validating Json Web Tokens. See http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-07.
public class JwtSecurityTokenHandler
它有一个方法叫 “WriteToken” 与描述为:
// Summary:
// Writes the System.IdentityModel.Tokens.JwtSecurityToken as a JSON Compact
// serialized format string.
//
//
// Returns:
// The System.IdentityModel.Tokens.JwtSecurityToken as a signed (if System.IdentityModel.Tokens.SigningCredentials
// exist) encoded string.
请问这个转换为:“常规”智威汤逊,可以通过客户端浏览器的消耗和解析?
ClaimsIdentity sofCid = new ClaimsIdentity(new List<Claim>() { new Claim("MyClaimType", "MyClaimValue") }, "Custom");
var descriptor = new SecurityTokenDescriptor
{
Subject = cid,
AppliesToAddress = "http://myappliesto.com",
SigningCredentials = new X509SigningCredentials(/* code that looks up the cert */),
TokenIssuerName = "http://myissuer.com",
Lifetime = new Lifetime(DateTime.UtcNow, DateTime.UtcNow.AddMinutes(20))
};
JwtSecurityTokenHandler jwtHandler = new JwtSecurityTokenHandler();
SecurityToken jwt = jwtHandler.CreateToken(descriptor);
string jsonCompactSerializedString = jwtHandler.WriteToken(jwt);
所以当我说“正常”的JSON,像下面:
{
"isCool":true,
"favoriteColor":"green",
"rocksTheSuburbs":true,
"favoriteDecade":"80",
"isHipToBeSquare":true
}
或
{
"iss": "jira:1314039",
"iat": 1300819370,
"exp": 1300819380,
"qsh": "8063ff4ca1e41df7bc90c8ab6d0f6207d491cf6dad7c66ea797b4614b71922e9",
"sub": "batman",
"context": {
"user": {
"userKey": "batman",
"username": "bwayne",
"displayName": "Bruce Wayne"
}
}
}