Gleeman.JwtGenerator
1.0.1
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 Gleeman.JwtGenerator --version 1.0.1
NuGet\Install-Package Gleeman.JwtGenerator -Version 1.0.1
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="Gleeman.JwtGenerator" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gleeman.JwtGenerator --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Gleeman.JwtGenerator, 1.0.1"
#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 Gleeman.JwtGenerator as a Cake Addin #addin nuget:?package=Gleeman.JwtGenerator&version=1.0.1 // Install Gleeman.JwtGenerator as a Cake Tool #tool nuget:?package=Gleeman.JwtGenerator&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Gleeman.JwtGenerator
HOW TO USE ?
appsettings.json
"TokenSetting": {
"SaveToken": true, // true or false
"ValidateIssuer": false, // true or false
"ValidateAudience": false, // true or false
"ValidateLifetime": true, // true or false
"Issuer": null, // string
"Audience": null, // string
"SigningKey": "You should be write here your security key!" // string
}
Program.cs
builder.Services.AddJwtGenerator(builder.Configuration);
app.UseAuthentication();
app.UseAuthorization();
EXAMPLE
appsettings.json
"TokenSetting": {
"SaveToken": true,
"ValidateIssuer": true,
"ValidateAudience": true,
"ValidateLifetime": true,
"Issuer": "This is issuer",
"Audience": "This is audience",
"SigningKey": "This is our security key"
}
Program.cs
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseInMemoryDatabase("TestDb"));
builder.Services.AddJwtGenerator(builder.Configuration);
builder.Services.AddScoped<IUserService, UserService>();
Database.AddUserData(app);
app.UseAuthentication();
app.UseAuthorization();
Service
public interface IUserService
{
Task<LoginResponse> LoginAsync(LoginRequest loginRequest);
}
public class UserService : IUserService
{
private readonly ITokenGenerator _tokenGenerator;
private readonly AppDbContext _dbContext;
public UserService(ITokenGenerator tokenGenerator, AppDbContext dbContext)
{
_tokenGenerator = tokenGenerator;
_dbContext = dbContext;
}
public async Task<LoginResponse> LoginAsync(LoginRequest loginRequest)
{
var user = await _dbContext.Users
.Where(x => x.Email == loginRequest.Email && x.Password == loginRequest.Password)
.Include(x => x.Role)
.SingleOrDefaultAsync();
if (user == null)
{
return new LoginResponseMessage("Email or Password is wrong!") { Success = false };
}
var accessToken = _tokenGenerator.GenerateAccessToken(
new TokenParameter(
email:user.Email,
username:user.UserName!,
role:user.Role.RoleName,
dateofBirth:user.DateOfBirth.ToString()!,
mobilePhone:user.PhoneNumber!),
ExpireType.Minute,
5);
var refreshToken = _tokenGenerator.GenerateRefreshToken(
ExpireType.Minute,
10);
user.Token = refreshToken.Token;
user.TokenExpire = refreshToken.ExpireDate;
_dbContext.Update(user);
await _dbContext.SaveChangesAsync();
return new LoginResponse
{
AccessToken = accessToken.Token,
AccessExpires = accessToken.ExpireDate,
RefreshToken = refreshToken.Token,
RefreshExpires = refreshToken.ExpireDate,
Success = true
};
}
}
Controller
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
private readonly IUserService _userService;
public AuthController(IUserService userService)
{
_userService = userService;
}
[HttpPost]
public async Task<IActionResult> Login(LoginRequest loginRequest)
{
var result = await _userService.LoginAsync(loginRequest);
if (result.Success)
{
return Ok(new { AccessToken = result.AccessToken, AccessExpire = result.AccessExpires, RefreshToken = result.RefreshToken, RefreshExpires = result.RefreshExpires });
}
return BadRequest(result.Message);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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.
-
net7.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.