Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

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

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
    AliGul 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
    Martin Dráb 230,198 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans