Conflict between System.IdentityModel.Tokens and Microsoft.IdentityModel.Tokens

荒凉一梦 提交于 2019-12-20 08:24:11

问题


I have a conflict when using 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)
}

I get following error when I create header (var header = new JwtHeader(signingCredentials);) :

Argument type 'System.IdentityModel.Tokens.SigningCredentials' is not assignable to parameter type 'Microsoft.IdentityModel.Tokens.SigningCredentials'

I don't understand because all my type refers to System.IdentityModel.Tokens. and in documentation JwtHeader Constructor need System.IdentityModel.Tokens.SigningCredentials

I don't know what's wrong ...


回答1:


System.IdentityModel.Tokens.Jwt version 5.0.0.0 depends on Microsoft.IdentityModel.Tokens.

You need to use SigningCredentials in the Microsoft.IdentityModel.Tokens namespace.

Example:

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



回答2:


May be you are using Jwt version 5.0.0.0 or above. I have faced the same issue before.

The new version of JWT handler accepts Microsoft.IdentityModel.Tokens namespace.

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


来源:https://stackoverflow.com/questions/38338580/conflict-between-system-identitymodel-tokens-and-microsoft-identitymodel-tokens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!