Skip to content

codingstill/dotnetcore-jwt-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT C# library

A C# class that can sign and validate JWT tokens, wrapped in a simple library with a couple of helper functions.

RSA Algorithm

To generate a compatible private key

openssl genrsa -out private.key 4096

To genarate a compatible certificate from the private key

openssl req -new -x509 -key private.key -out cert.pem -days 360

To generate a compatible public key from the private key

openssl rsa -in private.key -outform PEM -pubout -out public.pem

Sign a JWT token

using Newtonsoft.Json;

JwtManager.RsJwt jwt = new JwtManager.RsJwt
{
    KeySize = JwtManager.Helpers.KeySize.S256, // This can be also S384 or S512
    PrivateKey = PrivateKey
};

string strToken = JsonConvert.SerializeObject(myToken);
string signedToken = jwt.Sign(strToken);

In case of an error, an Exception will be thrown.

Validate a JWT token

using Newtonsoft.Json;

JwtManager.RsJwt jwt = new JwtManager.RsJwt
{
    KeySize = JwtManager.Helpers.KeySize.S256, // This can be also S384 or S512
    PublicKey = PublicKey
};

string payload = jwt.Validate(strToken);
var myToken = JsonConvert.DeserializeObject<JwtToken>(payload);

Validate a JWT token with a certificate

using Newtonsoft.Json;

JwtManager.RsJwt jwt = new JwtManager.RsJwt
{
    KeySize = JwtManager.Helpers.KeySize.S256, // This can be also S384 or S512
    Certificate = Certificate
};

string payload = jwt.Validate(strToken);
var myToken = JsonConvert.DeserializeObject<JwtToken>(payload);

In case of an error, an Exception will be thrown.

HMAC Algorithm

For this you need a secret in a string variable. Longer secret is better

Sign a JWT token

using Newtonsoft.Json;

string secret = "setyourverysecretkeyhere";

JwtManager.HsJwt jwt = new JwtManager.HsJwt
{
    KeySize = JwtManager.Helpers.KeySize.S256, // This can be also S384 or S512
    Secret = secret
};

string strToken = JsonConvert.SerializeObject(myToken);
string signedToken = jwt.Sign(strToken);

In case of an error, an Exception will be thrown.

Validate a JWT token

using Newtonsoft.Json;

string secret = "setyourverysecretkeyhere";

JwtManager.HsJwt jwt = new JwtManager.HsJwt
{
    KeySize = JwtManager.Helpers.KeySize.S256, // This can be also S384 or S512
    Secret = secret
};

string payload = jwt.Validate(strToken);
var myToken = JsonConvert.DeserializeObject<JwtToken>(payload);

In case of an error, an Exception will be thrown.

Other Info

The code has been tested both as a .NET and .NET Core library.

Check the JwtManagerTests project for more examples on how to use

About

A Jwt Manager in C# for .NET Core projects

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy