Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Could not load type 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'

(0) ShareShare
ReportReport
Posted on by
Hi Folks i'm trying to generate JWT token.
 
for that i created classLibrary and used it in d365 fo projects reference to generate the JWT token but it is showing below error.

Could not load type 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken' from assembly 'System.IdentityModel.Tokens.Jwt, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
 
the code i used in DLL is 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
namespace JWT
{
    public class getToken
    {
        
        public string getJWTToken(string apiKey, string secret)
        {
           
            string tokenCreationTime = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            //Your payload
            var claims = new[]
            {
            new Claim(/iss/,apiKey),
            new Claim(/iat/, tokenCreationTime)
            };
            //Your secret key for signing the token
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(secret);
            string base64String = Convert.ToBase64String(bytes);

            var key = new SymmetricSecurityKey(Convert.FromBase64String(base64String));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken(
                issuer: apiKey,
                claims: claims //,
                 //expires: DateTime.Now.AddHours(1), // Adjust as needed
                signingCredentials: creds
            );
            string tokenString = new JwtSecurityTokenHandler().WriteToken(token);
            return tokenString;

        }
    }
}
and the code i used in x++ is
 
using JWT;
internal final class JWTGenerate
{
    public static void main(Args _args)
    {
        JWT.getToken getToken;
       
        getToken = new JWT.getToken();

        str    api = '********************',
            secret = '********************';
 
        str token = getToken.getJWTToken(api,secret).ToString();
      
        info(strFmt(/%1/, token));
    }
}
 
showing i  tried same code in C# console app it is working perfectly.
  • AliGul Profile Picture
    64 on at
    Could not load type 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'
    Hi everybody,

    I am facing same issue, has anyone resolved this? please help.
  • Suggested answer
    Martin Dráb Profile Picture
    231,923 Most Valuable Professional on at
    Could not load type 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'
    F&O indeed doesn't contain this assembly. Add it to the bin folder of your model and add a reference to it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,238 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,923 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans