Edi.AspNetCore.Jwt 1.3.0

dotnet add package Edi.AspNetCore.Jwt --version 1.3.0                
NuGet\Install-Package Edi.AspNetCore.Jwt -Version 1.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Edi.AspNetCore.Jwt" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Edi.AspNetCore.Jwt --version 1.3.0                
#r "nuget: Edi.AspNetCore.Jwt, 1.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Edi.AspNetCore.Jwt as a Cake Addin
#addin nuget:?package=Edi.AspNetCore.Jwt&version=1.3.0

// Install Edi.AspNetCore.Jwt as a Cake Tool
#tool nuget:?package=Edi.AspNetCore.Jwt&version=1.3.0                

Edi.AspNetCore.Jwt

.NET Build and Pack

A very basic JWT authentication library for quickly integrating JWT authentication in ASP.NET Core applications.

Install

dotnet add package Edi.AspNetCore.Jwt

For SQL Server token storage, also

dotnet add package Edi.AspNetCore.Jwt.SqlServer

Usage

ASP.NET Core

Program.cs
builder.Services.AddJwtAuth<DefaultJwtAuthManager>(builder.Configuration)
                .AddInMemoryRefreshTokenStore(); 
                // For SQL Server token storage, also 
                // .AddSqlServerRefreshTokenStore("DefaultConnection");
appsettings.json
"JWTConfig": {
  "Secret": "b9e942ac-dd01-4523-9e0e-21f4f642fe74",
  "Issuer": "edi.wang",
  "Audience": "edi",
  "AccessTokenExpiration": 20,
  "RefreshTokenExpiration": 480
}

For SQL Server token storage, also

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=work996;Trusted_Connection=True;"
}
DI
private readonly IJwtAuthManager _jwtAuthManager;

public AccountController(IJwtAuthManager jwtAuthManager)
{
    _jwtAuthManager = jwtAuthManager;
}
Sign In
var claims = new List<Claim>
    {
        new("UserId", user.Id.ToString()),
        new(ClaimTypes.Name, user.DisplayName),
        new(ClaimTypes.Email, request.Email),
    };
claims.AddRange(user.Roles.Select(role => (Claim)new(ClaimTypes.Role, role.ShortCode)));

var jwtResult = _jwtAuthManager.GenerateTokens(request.Email, claims.ToArray(), DateTime.UtcNow);

SetRefreshTokenCookie(jwtResult.RefreshToken.TokenString);

return Ok(new LoginResult
{
    AccessToken = jwtResult.AccessToken.TokenString
});
private void SetRefreshTokenCookie(string token)
{
    var cookieOptions = new CookieOptions
    {
        Secure = true,
        SameSite = SameSiteMode.None,
        HttpOnly = true,
        Expires = DateTime.UtcNow.AddMinutes(_jwtAuthManager.JwtTokenConfig.RefreshTokenExpiration)
    };
    Response.Cookies.Append("refreshToken", token, cookieOptions);
}
Refresh Token
var refreshToken = Request.Cookies["refreshToken"];

var hasAuthHeader = Request.Headers.TryGetValue("Authorization", out var authHeaderValue);
if (!hasAuthHeader) return Unauthorized("No Authorization header");

var parseAuthHeader = AuthenticationHeaderValue.TryParse(authHeaderValue, out var accessToken);
if (!parseAuthHeader) return Unauthorized("Unable to parse Authorization header");

var jwtResult = _jwtAuthManager.Refresh(refreshToken, accessToken.Parameter, ClaimTypes.Email, DateTime.UtcNow);

SetRefreshTokenCookie(jwtResult.RefreshToken.TokenString);

return Ok(jwtResult);
Sign Out
var email = User.FindFirst(p => p.Type == ClaimTypes.Email)?.Value;
_jwtAuthManager.RemoveRefreshToken(email);

免责申明

此项目及其配套组件均为免费开源的产品,仅用于学习交流,并且不直接向中国提供服务,中国访客请于下载后立即删除。

任何中国境内的组织及个人不得使用此项目及其配套组件构建任何形式的面向中国境内访客的网站或服务。

不可用于任何违反中华人民共和国(含台湾省)或使用者所在地区法律法规的用途。

因为作者即本人仅完成代码的开发和开源活动(开源即任何人都可以下载使用),从未参与用户的任何运营和盈利活动。

且不知晓用户后续将程序源代码用于何种用途,故用户使用过程中所带来的任何法律责任即由用户自己承担。

《开源软件有漏洞,作者需要负责吗?是的!》

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Edi.AspNetCore.Jwt:

Package Downloads
Edi.AspNetCore.Jwt.SqlServer

SQL Server storage provider for Edi.AspNetCore.Jwt

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 129 11/13/2024
1.2.1 107 10/28/2024
1.2.0 120 9/24/2024
1.0.1 88 9/24/2024
1.0.0 163 8/15/2024
0.12.2 131 8/14/2024
0.12.1 76 8/5/2024
0.12.0 171 7/1/2024
0.11.0 155 5/7/2024
0.10.0 206 4/24/2024
0.10.0-beta.9 101 4/10/2024
0.10.0-beta.8 100 3/26/2024
0.9.7-beta.7 76 3/25/2024