53

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 ...

Community
  • 1
  • 1
Cooxkie
  • 6,740
  • 6
  • 22
  • 26

2 Answers2

59

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);
}
Emilio Brandon Flanagan
  • 1,142
  • 1
  • 12
  • 13
  • OK thanks ! The matter is that my third part where I will send my token accept only RS256 Algorythm ... – Cooxkie Jul 13 '16 at 06:41
  • @Cooxkie: Hi! I'm having the same problem as you. Been following this: http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/. I wanted to ask... How did you generate the SymmetricSecurityKey? Thanks. – Jose A Jul 25 '16 at 01:29
  • @JoseA : I changed my mind, and now I used p12 certificate (Asymetric key), sorry :-(. – Cooxkie Jul 25 '16 at 08:11
  • 1
    @JoseA : But I remember that I found solutions to generate symetric Key. Check answer from Kastorskij here : http://stackoverflow.com/questions/14735753/how-to-configure-microsoft-jwt-with-symmetric-key – Cooxkie Jul 25 '16 at 08:12
  • 3
    You seem to have broken something important. The 4.0.1 version of the libraries work just fine with the ConfigurationManager calls to get the V 2.0 endpoint metadata. But when you upgrade to 5.1.2 the deserialization attempts to construct a **DateTimeUtil** structure which no longer exists: **Additional information: Could not load type 'System.IdentityModel.DateTimeUtil' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'** – Quark Soup Feb 18 '17 at 23:34
7

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);
SharmaPattar
  • 2,472
  • 3
  • 21
  • 23