Edi.AspNetCore.Jwt.SqlServer
0.12.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Edi.AspNetCore.Jwt.SqlServer --version 0.12.0
NuGet\Install-Package Edi.AspNetCore.Jwt.SqlServer -Version 0.12.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.SqlServer" Version="0.12.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Edi.AspNetCore.Jwt.SqlServer --version 0.12.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Edi.AspNetCore.Jwt.SqlServer, 0.12.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.SqlServer as a Cake Addin #addin nuget:?package=Edi.AspNetCore.Jwt.SqlServer&version=0.12.0 // Install Edi.AspNetCore.Jwt.SqlServer as a Cake Tool #tool nuget:?package=Edi.AspNetCore.Jwt.SqlServer&version=0.12.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Edi.AspNetCore.Jwt (>= 0.10.0)
- Microsoft.Data.SqlClient (>= 5.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.