remove this Microsoft.IdentityModel.Tokens then the jwt is worked
This commit is contained in:
+81
-78
@@ -9,91 +9,94 @@ using CarParkValidationAPI.Entities;
|
||||
using CarParkValidationAPI.Interface;
|
||||
using CarParkValidationAPI.Models;
|
||||
|
||||
namespace CarParkValidationAPI.Authorization
|
||||
namespace CarParkValidationAPI.Authorization;
|
||||
|
||||
public class JwtUtils : IJwtUtils
|
||||
{
|
||||
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public class JwtUtils : IJwtUtils
|
||||
public JwtUtils(IOptions<AppSettings> appSettings)
|
||||
{
|
||||
private readonly AppSettings _appSettings;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
|
||||
public JwtUtils(IOptions<AppSettings> appSettings)
|
||||
public string GenerateJwtToken(User user)
|
||||
{
|
||||
// generate token that is valid for 2 days
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
|
||||
public string GenerateJwtToken(User user)
|
||||
{
|
||||
// generate token that is valid for 2 days
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new[] {
|
||||
new Claim("id", user.Id.ToString()),
|
||||
new Claim("subject", user.Username)
|
||||
|
||||
//add more if we need it
|
||||
}
|
||||
),
|
||||
Expires = DateTime.UtcNow.AddDays(2),
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
// get back id and username. in subject
|
||||
public User? ValidateJwtToken(string token)
|
||||
{
|
||||
if (token == null)
|
||||
return null;
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
|
||||
try
|
||||
{
|
||||
tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false,
|
||||
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
|
||||
ClockSkew = TimeSpan.Zero
|
||||
}, out SecurityToken validatedToken);
|
||||
|
||||
var jwtToken = (JwtSecurityToken)validatedToken;
|
||||
// var userId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value); //old
|
||||
int userId = 0;
|
||||
string username = "";
|
||||
Claim claim;
|
||||
for (int i = 0; i< jwtToken.Claims.Count(); i++)
|
||||
{
|
||||
claim = jwtToken.Claims.ElementAt(i);
|
||||
if (claim.Type == "id")
|
||||
{
|
||||
userId = int.Parse(claim.Value);
|
||||
}
|
||||
else if (claim.Type == "subject")
|
||||
{
|
||||
username = claim.Value;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(username) && userId > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.Id = userId;
|
||||
user.Username = username;
|
||||
|
||||
// return user id from JWT token if validation successful
|
||||
return user;
|
||||
Subject = new ClaimsIdentity(new[] {
|
||||
new Claim("id", user.Id.ToString()),
|
||||
new Claim("subject", user.Username)
|
||||
|
||||
//add more if we need it
|
||||
}
|
||||
catch
|
||||
),
|
||||
Expires = DateTime.UtcNow.AddDays(2),
|
||||
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
||||
};
|
||||
var token = tokenHandler.CreateToken(tokenDescriptor);
|
||||
return tokenHandler.WriteToken(token);
|
||||
}
|
||||
// get back id and username. in subject
|
||||
public User? ValidateJwtToken(string token)
|
||||
{
|
||||
if (token == null)
|
||||
return null;
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
|
||||
try
|
||||
{
|
||||
tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = false,
|
||||
ValidateLifetime = true,
|
||||
ValidateAudience = false,
|
||||
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
|
||||
// ClockSkew = TimeSpan.Zero
|
||||
ClockSkew = TimeSpan.FromDays(2),
|
||||
}, out SecurityToken validatedToken);
|
||||
|
||||
var jwtToken = (JwtSecurityToken)validatedToken;
|
||||
// var userId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value); //old
|
||||
int userId = 0;
|
||||
string username = "";
|
||||
Claim claim;
|
||||
for (int i = 0; i < jwtToken.Claims.Count(); i++)
|
||||
{
|
||||
claim = jwtToken.Claims.ElementAt(i);
|
||||
if (claim.Type == "id")
|
||||
{
|
||||
userId = int.Parse(claim.Value);
|
||||
}
|
||||
else if (claim.Type == "subject")
|
||||
{
|
||||
username = claim.Value;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(username) && userId > 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.Id = userId;
|
||||
user.Username = username;
|
||||
|
||||
// return user id from JWT token if validation successful
|
||||
return user;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
// return null if validation fails
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user