dnx451 RC1 InMemorySymmetricSecurityKey 发生了什么?

dnx451 RC1 What happened to InMemorySymmetricSecurityKey?

我一直在尝试使用简单的密钥创建和签署 JwtSecurityToken。经过大量研究,我发现的所有示例似乎都使用 InMemorySymmetricSecurityKey class 但不幸的是,这个 class 似乎不存在于 [=25= 的最新版本中]System.IdentityModel 个库。

这些是我正在使用的依赖项:

"System.IdentityModel.Tokens": "5.0.0-rc1-211161024",
"System.IdentityModel.Tokens.Jwt": "5.0.0-rc1-211161024"

我也尝试使用它的基础 class SymmetricSecurityKey 但是在尝试创建令牌时出现以下异常:

"Value cannot be null.\r\nParameter name: IDX10000: The parameter 'signatureProvider' cannot be a 'null' or an empty object."

这是抛出异常的代码:

public static string CreateTokenHMAC()
{
    HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String("test"));

    var key = new SymmetricSecurityKey(hmac.Key);

    var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);

    JwtSecurityToken token = _tokenHandler.CreateJwtSecurityToken(new SecurityTokenDescriptor()
    {
        Audience = AUDIENCE,
        Issuer = ISSUER,
        Expires = DateTime.UtcNow.AddHours(6),
        NotBefore = DateTime.Now,
        Claims = new List<Claim>()
        {
            new Claim(ClaimTypes.Email, "johndoe@example.com")
        },
        SigningCredentials = signingCredentials
    });

    return _tokenHandler.WriteToken(token);
}

这是我第一次使用 JwtSecurityToken,所以我猜我可能在某处遗漏了一步

我设法遇到了完全相同的异常。我通过另一种方式生成密钥解决了这个问题:

RSAParameters keyParams;
using (var rsa = new RSACryptoServiceProvider(2048))
{
    try
    {
        keyParams = rsa.ExportParameters(true);
    }
    finally
    {
        rsa.PersistKeyInCsp = false;
    }
}
RsaSecurityKey key = new RsaSecurityKey(keyParams);
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);

这里有一篇关于 by Mark Hughes

的精彩 post

这应该有效(注意这需要 RC2 包 > 304180813)

var handler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
    Subject = new ClaimsIdentity(
        new Claim[] { new Claim(ClaimTypes.NameIdentifier, "bob") }),
    SigningCredentials = new SigningCredentials(
        new SymmetricSecurityKey(new byte[256]),
        SecurityAlgorithms.HmacSha256)
};

var jwt = handler.CreateEncodedJwt(tokenDescriptor);

我无法使用已接受答案中提供的 RsaSecurityKey 示例让它工作,但这对我有用(使用 System.IdentityModel.Tokens.Jwt v5.1.3)。

var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("test"));
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);

var securityTokenDescriptor = new SecurityTokenDescriptor()
{
    Subject = new ClaimsIdentity(new List<Claim>()
    {
        new Claim(ClaimTypes.NameIdentifier, "johndoe@example.com"),
        new Claim(ClaimTypes.Role, "Administrator"),
    }, "Custom"),
    NotBefore = DateTime.Now,
    SigningCredentials = signingCredentials,
    Issuer = "self",
    IssuedAt = DateTime.Now,
    Expires = DateTime.Now.AddHours(3),
    Audience = "http://my.website.com"
};

var tokenHandler = new JwtSecurityTokenHandler();
var plainToken = tokenHandler.CreateToken(securityTokenDescriptor);
var signedAndEncodedToken = tokenHandler.WriteToken(plainToken);

并验证

var validationParameters = new TokenValidationParameters()
{
     ValidateAudience = true,
     ValidAudience = "http://my.website.com",
     ValidateIssuer = true,
     ValidIssuer = "self",
     ValidateIssuerSigningKey = true,
     IssuerSigningKey = signingKey,
     RequireExpirationTime = true,
     ValidateLifetime = true,
     ClockSkew = TimeSpan.Zero
};
try
{
    SecurityToken mytoken = new JwtSecurityToken();
    var myTokenHandler = new JwtSecurityTokenHandler();
    var myPrincipal = myTokenHandler.ValidateToken(signedAndEncodedToken, validationParameters, out mytoken);
} catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("Authentication failed");
}