System.IdentityModel.Tokens与Microsoft.IdentityModel.Tokens

时间:2016-07-12 20:45:08

标签: c# token jwt

使用System.IdentityModel.Tokens:

时发生冲突
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

public voidGenereToken()
{
    const string sec = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
    var now = DateTime.UtcNow;
    var securityKey = new InMemorySymmetricSecurityKey(Encoding.Default.GetBytes(sec));
    var signingCredentials = new SigningCredentials(securityKey,
            SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest);

    var header = new JwtHeader(signingCredentials);

    var payload = new JwtPayload
    {
        {"iss", "a5fgde64-e84d-485a-be51-56e293d09a69"},
        {"scope", "https://example.com/ws"},
        {"aud", "https://example.com/oauth2/v1"},
        {"iat", now},
    };

    var secToken = new JwtSecurityToken(header, payload);

    var handler = new JwtSecurityTokenHandler();
    var tokenString = handler.WriteToken(secToken);
    Console.writeLine(tokenString)
}

创建标题时出现以下错误( var header = new JwtHeader(signingCredentials); ):

  

参数类型'System.IdentityModel.Tokens.SigningCredentials'不是   可分配给参数类型   'Microsoft.IdentityModel.Tokens.SigningCredentials'

我不明白,因为我的所有类型都引用了 System.IdentityModel.Tokens 。 在文档JwtHeader Constructor中需要 System.IdentityModel.Tokens.SigningCredentials

我不知道出了什么问题......

2 个答案:

答案 0 :(得分:54)

System.IdentityModel.Tokens.Jwt版本5.0.0.0依赖于Microsoft.IdentityModel.Tokens。

您需要在Microsoft.IdentityModel.Tokens命名空间中使用SigningCredentials。

示例:

using System;
using System.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

public void voidGenereToken() {
    const string sec = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
    var now = DateTime.UtcNow;
    var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.Default.GetBytes(sec));
    var signingCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(
        securityKey,
        SecurityAlgorithms.HmacSha256Signature);

    var header = new JwtHeader(signingCredentials);

    var payload = new JwtPayload
    {
            {"iss", "a5fgde64-e84d-485a-be51-56e293d09a69"},
            {"scope", "https://example.com/ws"},
            {"aud", "https://example.com/oauth2/v1"},
            {"iat", now},
        };

    var secToken = new JwtSecurityToken(header, payload);

    var handler = new JwtSecurityTokenHandler();
    var tokenString = handler.WriteToken(secToken);
    Console.WriteLine(tokenString);
}

答案 1 :(得分:6)

可能是您正在使用Jwt版本5.0.0.0或更高版本。我之前遇到过同样的问题。

新版本的JWT处理程序接受Microsoft.IdentityModel.Tokens名称空间。

var tokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor
            {
                Subject = claimsIdentity,
                Audience = allowedAudience,
                Issuer = issuerName,
                Expires = DateTime.MaxValue,
                SigningCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(
                    new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(symmetricKey), //symmetric key
                    System.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256Signature,
                    System.IdentityModel.Tokens.SecurityAlgorithms.Sha256Digest)
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.CreateToken(tokenDescriptor);