Mahesh Prajapati

Mahesh Prajapati

  • 1.6k
  • 120
  • 2k

How to Generate JSON Web Tokens (JWT) in asp.net core

Jul 1 2019 3:49 AM
I want to genrate JSON Web Tokens (JWT) for yodlee integration using ASP.net core
 

Answers (3)

1
Rajanikant Hawaldar

Rajanikant Hawaldar

  • 32
  • 38.8k
  • 438.4k
Jul 1 2019 4:02 AM
Hi Mahesh
 
Refer below links
 
To Generate JSON Web Tokens (JWT) in asp.net core
 
https://www.codepoc.io/blog/web-api/5694/simplest-way-to-generate-jwt-token-in-asp-net-core-web-api
 
https://developer.yodlee.com/docs/api/1.1/using_json_web_tokens%20
 
Thanks
1
Tejas Trivedi

Tejas Trivedi

  • 610
  • 1.8k
  • 181.5k
Jul 1 2019 4:00 AM
Hi Mahesh,
 
You need to use JWTSecurityTokenHandler class to generate JWT Token in ASP.Net core. Below is the documentation of the class
 
https://docs.microsoft.com/en-us/previous-versions/visualstudio/dn464181(v%3Dvs.114) 
 Below is the quick sample of what you need to do in order to generate the JWT token in ASP.Net core API 
 
https://salslab.com/a/jwt-authentication-and-authorisation-in-asp-net-core-web-api
 
Hope, this will help you out. 
0
dx dxdax

dx dxdax

  • 1.6k
  • 186
  • 1
Jan 27 2020 12:11 PM
Hi Mahesh 
 
the code i tried 
public string CreateToken()
{
string privateKey = File.ReadAllText(@"");
loginName = _configuration["Tokens:SandboxTestUser"];
issuerId = _configuration["Tokens:IssuerId"];
var t = DateTime.UtcNow - new DateTime(1970, 1, 1);
int iat = (int)t.TotalSeconds;
var payload = new JwtPayload
{
{ "iss", issuerId},
{ "iat", iat },
{ "exp", iat + 1800},
{ "sub", loginName }
};
RSAParameters rsaParams;
using (var tr = new StringReader(privateKey))
{
var pemReader = new PemReader(tr);
var KeyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();
var privateRsaParams = (RsaPrivateCrtKeyParameters)KeyParameter;
rsaParams = DotNetUtilities.ToRSAParameters(privateRsaParams);
}
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaParams);
// Dictionary<string, object> payload = claims.ToDictionary(k => k.Type, v => (object)v.Value);
return Jose.JWT.Encode(payload, rsa, Jose.JwsAlgorithm.RS512);
}
}