All Projects → codingstill → dotnetcore-jwt-manager

codingstill / dotnetcore-jwt-manager

Licence: MIT License
A Jwt Manager in C# for .NET Core projects

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to dotnetcore-jwt-manager

ChatService
ChatService (SignalR).
Stars: ✭ 26 (+36.84%)
Mutual labels:  dot-net-core
simple-blog-back
Back-End for Simple Blog
Stars: ✭ 36 (+89.47%)
Mutual labels:  dot-net-core
Simplify.Web
Moved to https://github.com/SimplifyNet. Simplify.Web is a lightweight and fast server-side .NET web-framework based on MVC and OWIN for building HTTP based web-applications, RESTful APIs etc.
Stars: ✭ 23 (+21.05%)
Mutual labels:  dot-net-core
FineCodeCoverage
Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too)
Stars: ✭ 391 (+1957.89%)
Mutual labels:  dot-net-core
ContosoLending
An ASP.NET Core 3.1 app showcasing gRPC, server-side Blazor, SignalR, and C# 8.
Stars: ✭ 15 (-21.05%)
Mutual labels:  dot-net-core
SQL-To-ElasticSearch
SQL to ElasticSearch Query Converter
Stars: ✭ 40 (+110.53%)
Mutual labels:  dot-net-core

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 generate a compatible public 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);

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].