How to resolve the error "the type 'SecurityTokenDescriptor' exists in both" in C# code?

Sreeram Jayaram 0 Reputation points
2024-01-02T11:10:18.73+00:00
public string GenerateClientAccessToken(string hubName, string userId = null, IList<Claim> claims = null, TimeSpan? lifeTime = null)
{
    var tokenLifetime = lifeTime ?? TimeSpan.FromHours(1); // Default lifetime is one hour
    var claimsIdentity = new ClaimsIdentity(claims ?? Array.Empty<Claim>());

    if (!string.IsNullOrEmpty(userId))
    {
        claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId));
    }

    var tokenHandler = new JwtSecurityTokenHandler();
    var tokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor
    {
        Expires = DateTime.UtcNow.Add(tokenLifetime),
        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Guid.NewGuid().ToByteArray()), SecurityAlgorithms.HmacSha256),
        Subject = claimsIdentity,
    };

    var token = tokenHandler.CreateToken(tokenDescriptor);
    return tokenHandler.WriteToken(token);
}

I'm using this method to generate client access token for SignalR, but I'm unable to use 'SecurityTokenDescriptor' because it shows "the type 'SecurityTokenDescriptor' exists in both 'Microsoft.IdentityModel.Tokens, Version=6.10.0.0, Culture=neural' and 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral'". How can I resolve this error?

Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
120 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,285 questions
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,256 Reputation points Microsoft Employee
    2024-01-03T00:26:39.4966667+00:00

    @Sreeram Jayaram This error occurs because the SecurityTokenDescriptor class is defined in both Microsoft.IdentityModel.Tokens and Microsoft.IdentityModel namespaces. To resolve this error, you can use the fully qualified name of the SecurityTokenDescriptor class in your code.

    Replace the line var tokenDescriptor = new Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor with var tokenDescriptor = new System.IdentityModel.Tokens.Jwt.SecurityTokenDescriptor.

    This will use the SecurityTokenDescriptor class from the System.IdentityModel.Tokens.Jwt namespace instead of the Microsoft.IdentityModel.Tokens namespace.

    Here's the updated code:

    public string GenerateClientAccessToken(string hubName, string userId = null, IList claims = null, TimeSpan? lifeTime = null)
    {
        var tokenLifetime = lifeTime ?? TimeSpan.FromHours(1); // Default lifetime is one hour
        var claimsIdentity = new ClaimsIdentity(claims ?? Array.Empty());
    
        if (!string.IsNullOrEmpty(userId))
        {
            claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId));
        }
    
        var tokenHandler = new JwtSecurityTokenHandler();
        var tokenDescriptor = new System.IdentityModel.Tokens.Jwt.SecurityTokenDescriptor
        {
            Expires = DateTime.UtcNow.Add(tokenLifetime),
            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Guid.NewGuid().ToByteArray()), SecurityAlgorithms.HmacSha256),
            Subject = claimsIdentity,
        };
    
        var token = tokenHandler.CreateToken(tokenDescriptor);
        return tokenHandler.WriteToken(token);
    }
    
    
    
    0 comments No comments