Search code examples
c#jwt

Could not load file or assembly Microsoft.IdentityModel.Tokens problem


I am trying to verify users with a JWT token. The code I used below works perfectly fine in a console application. But when I want to apply it in my Azure function it gives me the error:

Could not load file or assembly Microsoft.IdentityModel.Tokens

I do have one other Azure function in my solution but it doesn't use this NuGet package. I already took a look at this link:

Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=5.2.0.0

I can't get anything out of that. So what am I doing wrong? Thanks in advance

string key = "";
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

var header = new JwtHeader(credentials);

var payload = new JwtPayload
{
    { "some ", "hello "},
    { "scope", "http://dummy.com/"},
};

var secToken = new JwtSecurityToken(header, payload);
var handler = new JwtSecurityTokenHandler();

var tokenString = handler.WriteToken(secToken);

var token = handler.ReadJwtToken(tokenString);

log.LogInformation(token.ToString());

Solution

  • Solved it by adding a line of code in the .csproj file

    <PropertyGroup>
        <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
    </PropertyGroup>