add lastmodified and lastmodifiedid to table also add code for modified and create

This commit is contained in:
Kham
2021-09-01 16:03:05 +10:00
parent c5ab17115d
commit cc276d4a17
25 changed files with 255 additions and 115 deletions
@@ -21,13 +21,13 @@ namespace CarParkValidationAPI.Authorization
public async Task Invoke(HttpContext context, IUserService userService, IJwtUtils jwtUtils)
{
var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
var userId = jwtUtils.ValidateJwtToken(token);
if (userId != null)
var user = jwtUtils.ValidateJwtToken(token);
if (user != null)
{
// attach user to context on successful jwt validation
//here to put in the real user
//TODO if you want to add information for User
// context.Items["User"] = userService.GetById(userId.Value);
context.Items["User"] = user;
}
await _next(context);
+31 -5
View File
@@ -30,7 +30,9 @@ namespace CarParkValidationAPI.Authorization
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] {
new Claim("id", user.Id.ToString())
new Claim("id", user.Id.ToString()),
new Claim("subject", user.Username)
//add more if we need it
}
),
@@ -40,8 +42,8 @@ namespace CarParkValidationAPI.Authorization
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
public int? ValidateJwtToken(string token)
// get back id and username. in subject
public User? ValidateJwtToken(string token)
{
if (token == null)
return null;
@@ -61,10 +63,34 @@ namespace CarParkValidationAPI.Authorization
}, out SecurityToken validatedToken);
var jwtToken = (JwtSecurityToken)validatedToken;
var userId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);
// 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 userId;
return user;
}
catch
{