System.IdentityModel.Tokens和Microsoft.IdentityModel.Tokens之间的冲突

53

使用 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个回答

59

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);
}

好的,谢谢!问题在于我要发送令牌的第三部分只接受RS256算法... - Cooxkie
@Cooxkie:你好!我和你一样遇到了同样的问题。我一直在关注这个链接:http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/。我想问一下...你是如何生成SymmetricSecurityKey的?谢谢。 - Jose A
@JoseA:我改变了主意,现在我使用了p12证书(非对称密钥),抱歉 :-(。 - Cooxkie
1
@JoseA:但我记得我找到了生成对称密钥的解决方案。在这里查看Kastorskij的答案:https://dev59.com/Y2Uq5IYBdhLWcg3wKtRL - Cooxkie
3
您似乎损坏了一些重要的东西。使用4.0.1版本的库可以很好地调用ConfigurationManager来获取V 2.0端点元数据。但是,当您升级到5.1.2时,反序列化会尝试构建一个不再存在的DateTimeUtil结构:**附加信息:无法从程序集'System.IdentityModel.Tokens.Jwt,Version=5.1.2.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'中加载类型'System.IdentityModel.DateTimeUtil'**。 - Quark Soup

7

可能您正在使用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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接