【问题标题】:Conflict between System.IdentityModel.Tokens and Microsoft.IdentityModel.TokensSystem.IdentityModel.Tokens 和 Microsoft.IdentityModel.Tokens 之间的冲突
【发布时间】:2016-11-15 06:26:06
【问题描述】:

我在使用 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

不知道怎么了……

【问题讨论】:

    标签: c# token jwt


    【解决方案1】:

    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:嗨!我和你有同样的问题。一直关注这个:bitoftech.net/2015/02/16/…。我想问...您是如何生成 SymmetricSecurityKey 的?谢谢。
    • @JoseA :我改变了主意,现在我使用了 p12 证书(非对称密钥),抱歉 :-(.
    • @JoseA :但我记得我找到了生成对称密钥的解决方案。在这里查看 Kastorskij 的答案:stackoverflow.com/questions/14735753/…
    • 你好像弄坏了一些重要的东西。库的 4.0.1 版本可以很好地与 ConfigurationManager 调用一起使用,以获取 V 2.0 端点元数据。但是当您升级到 5.1.2 时,反序列化会尝试构建一个不再存在的 DateTimeUtil 结构:附加信息:无法从程序集“System.IdentityModel.DateTimeUtil”加载类型“System.IdentityModel.DateTimeUtil”。 IdentityModel.Tokens.Jwt,版本=5.1.2.0,文化=中性,PublicKeyToken=31bf3856ad364e35'
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 2012-03-06
      • 2011-05-02
      • 2012-11-25
      • 2013-10-27
      • 2014-03-02
      • 2012-11-02
      • 2021-02-22
      • 2016-06-27
      相关资源
      最近更新 更多