All Projects → stulzq → Aspnetcore.authentication.apitoken

stulzq / Aspnetcore.authentication.apitoken

Licence: apache-2.0
A asp.net core webapi token authentication & generator open source library.

Projects that are alternatives of or similar to Aspnetcore.authentication.apitoken

Znetcs.aspnetcore.authentication.basic
A simple basic authentication middleware.
Stars: ✭ 40 (+53.85%)
Mutual labels:  aspnetcore, authentication
Idunno.authentication
A filled with self-loathing implementation of Basic Authentication, and Certificate Authentication to make me feel like a real security person, all for for ASP.NET Core
Stars: ✭ 228 (+776.92%)
Mutual labels:  aspnetcore, authentication
Aspnetcorewindowsauth
Local and Windows Authentication, ASP.NET Core MVC, IdentityServer4
Stars: ✭ 87 (+234.62%)
Mutual labels:  aspnetcore, authentication
Express Mongodb Rest Api Boilerplate
A boilerplate for Node.js apps / Rest API / Authentication from scratch - express, mongodb (mongoose).
Stars: ✭ 153 (+488.46%)
Mutual labels:  authentication, token
Paseto
Platform-Agnostic Security Tokens implementation in GO (Golang)
Stars: ✭ 461 (+1673.08%)
Mutual labels:  authentication, token
Paseto.js
PASETO: Platform-Agnostic Security Tokens
Stars: ✭ 241 (+826.92%)
Mutual labels:  authentication, token
Angularspawebapi
Angular Single Page Application with an ASP.NET Core Web API that uses token authentication
Stars: ✭ 222 (+753.85%)
Mutual labels:  aspnetcore, authentication
Paseto
Java Implementation of Platform-Agnostic Security Tokens - https://paseto.io
Stars: ✭ 32 (+23.08%)
Mutual labels:  authentication, token
Angular Token
🔑 Token based authentication service for Angular with interceptor and multi-user support. Works best with devise token auth for Rails. Example:
Stars: ✭ 376 (+1346.15%)
Mutual labels:  authentication, token
Express Graphql Mongodb Boilerplate
A boilerplate for Node.js apps / GraphQL-API / Authentication from scratch - express, graphql - (graphql compose), mongodb (mongoose).
Stars: ✭ 288 (+1007.69%)
Mutual labels:  authentication, token
Laravel Passport Android
Laravel + Passport for an Android App
Stars: ✭ 116 (+346.15%)
Mutual labels:  authentication, token
Aspnet5identityserverangularimplicitflow
OpenID Connect Code / Implicit Flow with Angular and ASP.NET Core 5 IdentityServer4
Stars: ✭ 670 (+2476.92%)
Mutual labels:  aspnetcore, authentication
Go Alone
A simple to use, high-performance, Go (golang) MAC signer.
Stars: ✭ 82 (+215.38%)
Mutual labels:  authentication, token
Dex K8s Authenticator
A Kubernetes Dex Client Authenticator
Stars: ✭ 249 (+857.69%)
Mutual labels:  authentication, token
Totp Cli
A cli-based pass-backed TOTP app
Stars: ✭ 76 (+192.31%)
Mutual labels:  authentication, token
Run Aspnetcore Realworld
E-Commerce real world example of run-aspnetcore ASP.NET Core web application. Implemented e-commerce domain with clean architecture for ASP.NET Core reference application, demonstrating a layered application architecture with DDD best practices. Download 100+ page eBook PDF from here ->
Stars: ✭ 208 (+700%)
Mutual labels:  aspnetcore, authentication
Access
Ponzu Addon to manage API access grants and tokens for authentication
Stars: ✭ 13 (-50%)
Mutual labels:  authentication, token
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (-7.69%)
Mutual labels:  aspnetcore, token
Django Graphql Jwt
JSON Web Token (JWT) authentication for Graphene Django
Stars: ✭ 649 (+2396.15%)
Mutual labels:  authentication, token
Github Create Token
Create a Github OAuth access token.
Stars: ✭ 6 (-76.92%)
Mutual labels:  authentication, token

AspNetCore.Authentication.ApiToken

English | 中文

Latest version

AspNetCore.Authentication.ApiToken is an authentication component for ASP.NET Core, following the design specification of ASP.NET Core authentication framework. It is mainly used in the WebApi project to provide issuance and verification Token capabilities. The Token issued by this component is not a Json Web Token (JWT), which is similar to the Reference Token in IdentityServer4 and needs to be queried on the server to verify the validity. If there is a need for Reference Token in IdentityServer4 in your project, then IdentityServer4 is recommended for medium and large projects. If it is a small and medium-sized project, then you can consider AspNetCore.Authentication.ApiToken, which is more portable than IdentityServer4. Maintenance costs are lower. The advantage of this Token over JWT is that it can completely control the life cycle of the Token. The disadvantage is that to verify the Token, you need to query the storage every time to compare and verify (the performance can be improved by caching).

Features

  • Simple access, only need to implement two interfaces
  • Integrated issuance, refresh, cancellation and verification of Token
  • Support caching, Redis is implemented by default, and other caches can be easily extended
  • Support regular cleaning of expired Token background tasks
  • Support to update the user claim (role) to take effect immediately without logging in again
  • Only one Token can be valid for the same user at the same time (if a new Token is issued, all old Tokens will become invalid)
  • Support smooth transition when refreshing Token, old Token will not be invalid immediately
  • Support authentication events

Quick start

1.Install

Install via Nuget in your WebApi project

dotnet add package AspNetCore.Authentication.ApiToken

2.Implementation interface IApiTokenProfileService

The main function of this interface is to query the user's Claims according to the user Id when creating and refreshing Tokens, such as commonly used: Name, Id, and Role.

The Claims provided here can be accessed in the HttpContext.User.Claims property after authentication is successful. Role Claim can be used on [Authorize], such as [Authorize(Roles = "Admin")]

Example(Entity Framework core):

MyApiTokenProfileService.cs

public class MyApiTokenProfileService : IApiTokenProfileService
{
    private readonly EfDbContext _dbContext;

    public MyApiTokenProfileService(EfDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    public async Task<List<Claim>> GetUserClaimsAsync(string userId)
    {
        var user = await _dbContext.Users.FirstAsync(a => a.Id == userId);
        return new List<Claim>()
        {
            new Claim(ApiTokenClaimTypes.Subject,userId),
            new Claim(ApiTokenClaimTypes.Name,user.Name),
            new Claim(ApiTokenClaimTypes.Role,user.Role),
        };
    }
}

3.Implementation interface IApiTokenStore

This interface is used to store, query, and delete tokens. Because the Token provided by this component needs to be checked and compared for validity verification.

The example uses the database as a storage implementation (Entity Framework core):

MqApiTokenStore.cs

public class MqApiTokenStore : IApiTokenStore
{
    //Store token
    public async Task StoreAsync(TokenModel token)
    {
        //...
    }

    //Store Token list
    public async Task StoreAsync(List<TokenModel> token)
    {
        //...
    }

    //Get token
    public async Task<TokenModel> GetAsync(string token, string scheme)
    {
        //...
    }

    //Get the token list
    public async Task<List<TokenModel>> GetListAsync(string userId, string scheme)
    {
        //...
    }

    //Get a list of tokens of the specified type
    public async Task<List<TokenModel>> GetListAsync(string userId, string scheme, TokenType type)
    {
        //...
    }

    //Update token
    public async Task UpdateAsync(TokenModel token)
    {
        //...
    }

    //Update token list
    public async Task UpdateListAsync(List<TokenModel> token)
    {
        //...
    }

    //Delete token
    public async Task RemoveAsync(string token, string scheme)
    {
        //...
    }

    //Delete list
    public async Task RemoveListAsync(string userId, string scheme)
    {
        //...
    }

    //Delete the Token list of the specified type
    public async Task RemoveListAsync(string userId, string scheme, TokenType type)
    {
        //...
    }

    //Remove expiration token
    public async Task<int> RemoveExpirationAsync()
    {
        //...
    }
}

4.Configuration

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(ApiTokenDefaults.AuthenticationScheme)
        .AddApiToken()
        .AddProfileService<MyApiTokenProfileService>()
        .AddTokenStore<MyApiTokenStore>();
    //Other services...
}

5.Issue token

You need to write an API for issuing tokens yourself.

Inject IApiTokenOperator tokenOperator

var createResult = await tokenOperator.CreateAsync("<userId>");

The returned result contains Bearer Token and Refresh Token. Bearer Token is used for interface verification, and Refresh Token is used for Token refresh.

6.Use Token

Similar to the way of using JWT, add Header to the request

Authorization: Bearer <token>

7.Demo

Please refer to the complete implementation SampleApp

Advance

1.Use cache

Install Nuget package:AspNetCore.Authentication.ApiToken.Redis

Add service on Startup.ConfigureServices AddRedisCache(op => op.ConnectionString = "<redis connection string>")

Example:

services.AddAuthentication(ApiTokenDefaults.AuthenticationScheme)
    .AddApiToken(op => op.UseCache = false)
    .AddRedisCache(op => op.ConnectionString = "127.0.0.1:6379")
    .AddProfileService<MyApiTokenProfileService>()
    .AddTokenStore<MyApiTokenStore>();

The cache validity period can be customized, generally the cache validity period is the same as the token expiration time.

2.Custom cache

To implement the IApiTokenCacheService interface, please refer to the implementation of Redis.

3.Clean Token Background Service

Periodic cleaning service refers to running to clean up expired tokens in the database at regular intervals, adding AddCleanService() to the registration service

Example:

services.AddAuthentication(ApiTokenDefaults.AuthenticationScheme)
    .AddApiToken(op => op.UseCache = false)
    .AddProfileService<MyApiTokenProfileService>()
    .AddTokenStore<MyApiTokenStore>()
    .AddCleanService();

Can customize the interval time.

4.Refresh Token

Inject IApiTokenOperator and call the RefreshAsync(string refreshToken, string scheme) method, it will automatically refresh and return the result.

The ApiTokenOptions.KeepTokenValidTimeSpanOnRefresh property can be used to set how long the old Token can be valid after refreshing.

5.Update claim

Inject IApiTokenOperator and call RefreshClaimsAsync(string token, string scheme) method. Mainly used for users to update information, such as name or role, if you do not need to login again, it will take effect immediately, you can call this method.

6.Revoke token

Inject IApiTokenOperator and call RemoveAsync(string token, string scheme) method.

Tips

The scheme in the above method can not be passed, but it needs to be passed in when multiple ApiToken authentication services are registered, or the ApiToken authentication is not the default scheme. This is because of the design of the authentication framework of ASP.NET Core. If you need to know the details, you can see the official documentation of ASP.NET Core.

Thanks

The following items are referred to in the design and compilation of this project:

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].