Edi.AspNetCore.Jwt
1.0.0
See the version list below for details.
dotnet add package Edi.AspNetCore.Jwt --version 1.0.0
NuGet\Install-Package Edi.AspNetCore.Jwt -Version 1.0.0
<PackageReference Include="Edi.AspNetCore.Jwt" Version="1.0.0" />
paket add Edi.AspNetCore.Jwt --version 1.0.0
#r "nuget: Edi.AspNetCore.Jwt, 1.0.0"
// Install Edi.AspNetCore.Jwt as a Cake Addin #addin nuget:?package=Edi.AspNetCore.Jwt&version=1.0.0 // Install Edi.AspNetCore.Jwt as a Cake Tool #tool nuget:?package=Edi.AspNetCore.Jwt&version=1.0.0
Edi.AspNetCore.Jwt
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 | Versions 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. |
-
net8.0
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 | 96 | 11/13/2024 |
1.2.1 | 100 | 10/28/2024 |
1.2.0 | 113 | 9/24/2024 |
1.0.1 | 81 | 9/24/2024 |
1.0.0 | 154 | 8/15/2024 |
0.12.2 | 122 | 8/14/2024 |
0.12.1 | 69 | 8/5/2024 |
0.12.0 | 164 | 7/1/2024 |
0.11.0 | 148 | 5/7/2024 |
0.10.0 | 198 | 4/24/2024 |
0.10.0-beta.9 | 98 | 4/10/2024 |
0.10.0-beta.8 | 97 | 3/26/2024 |
0.9.7-beta.7 | 73 | 3/25/2024 |