Get Start CSharpJWT | 424 day | 559

Note: The library is developed by the "" team and we are improving it day by day, if when use it you see any issue please do not hesitate to create an issue here, and we are always welcome your contributions.

1. What is CSharpJWT ?

The library helps you manage users, tenants. Besides, managing the lifetime of "access_token and refresh_token, revoke "access_token", sharing token between applications. Support "Authorization" attribute easily to validate "Audiences, Issuer, Roles...". So at the moment, a lot of frame-work to do it such as IdentityServer-4, or core Identity...

Summary: The library helps your application "Authentication".

2. How to do?

At the moment, we support nuget like below.

Install-Package CSharp.JWT -Version 2.2.0

 Then We need to copy the snippet code as below.

public void ConfigureServices(IServiceCollection services)
	services.AddCSharpJWTIdentity<CSharpJWTContext>(options =>
	   sql => sql.MigrationsAssembly("<Your Target Project>")));

Then using code first to generate a database.

Add-Migration InitialCreate

After the done step above we need to configure a "Distributed Cache" as below.

services.AddCSharpJWTDistributedMemoryCache(); ==> option 1

services.AddCSharpJWTDistributedSqlServerCache(options =>
    options.ConnectionString = Configuration.GetConnectionString("DistCache_ConnectionString");
    options.SchemaName = "dbo";
    options.TableName = "TestCache";
}); ==> option 2

 So here we have 2 options:

  1. Using memory.
  2. Using 1 SQL-Database to cache you can get more information from here.
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DistCache;Integrated Security=True;" dbo TestCache

The command above helps generate 1 cacheDB.

Next, add an authentication.


Next, we need to copy the snippet code below.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, SeedData seedData)



and a configuration like below

  "CSharpJWT": {
    "Issuer": "http://localhost:5000",
    "SecurityKey": <Your-Key>

With Security-key, it can a base64 or a string with length > 20 characters, with me usually use Putty to generate a Security-key.

The moment, we have done with an OAUTH-SERVER, and now we will test it.

Token: <your-host>:<your-port>:/csharp-token

Header: Content-Type: "application/x-www-form-urlencoded"


username: <Value>

password: <Value>

grant_type: password

Refresh Token: <your-host>:<your-port>:/csharp-token

Header: Content-Type: "application/x-www-form-urlencoded"


refresh_token: <Value>

grant_type: refresh_token

Revoke Token: <your-host>:<your-port>:/csharp-revoke-token

Header: Authorization: "Bearer <Your-Token>"

With Revoke-Token is usually use for logout.

Until now we have been completed with an OAuth-Server, we can create an API to test it.

    public class ValuesController : ControllerBase
        // GET api/values
        public ActionResult<IEnumerable<string>> Get()
            return new string[] { "value1", "value2" };

Here you can use the "[Authorize] from ms" or "[CSharpAuthorize]", both of them working. but with me refer to use "[CSharpAuthorize]" because it helps the system check token after call Revoke-Token.

So I hope the article helps you because you have another option to choose when building a web application and here is the source code.

If you don't understand or you need to ask something, do not hesitate to comment below, and next article I will show you how CSharpJWT can manage multi-tenants and share token between many websites.

Top Articles

Bất Đầu Với WebApi Và Dot Net Core (.Net Core)

1276 day
Butter Ngo
Views 8931
Comments 0

Dot Net Core Bearer Token With (JWT) (.Net Core)

1208 day
Butter Ngo
Views 5557
Comments 0

Repository Và Unit Of Work (Entity Framework)

1195 day
Views 5040
Comments 0

Bắt Đầu Với Dot NET Core (.Net Core)

1288 day
Butter Ngo
Views 4425
Comments 0

Top Question

Bi lỗi Invalid Column Name khi sử dụng LinQ (.Net)

1135 day
Bảo Dương
Views 1338
Answers 2


428 day
Views 940
Answers 2

Làm thế nào để lấy information từ token (.Net Core)

525 day
Views 851
Answers 1