private string GenerateJwtToken(UserForRetrievalUiModel registeredUser) { List <Claim> claims = new List <Claim> { new Claim(ClaimTypes.Email, registeredUser.Login), }; if (registeredUser.Roles != null) { foreach (var userRole in registeredUser.Roles) { claims.Add(new Claim(ClaimTypes.Role, userRole.Name)); } } var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(this._configuration.GetSection("TokenAuthentication:SecretKey").Value); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(claims), Expires = DateTime.Now.AddMinutes(int.Parse(this._configuration .GetSection("TokenAuthentication:ExpirationTimeInMinutes").Value)), Issuer = this._configuration.GetSection("TokenAuthentication:Issuer").Value, Audience = this._configuration.GetSection("TokenAuthentication:Audience").Value, SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); string tokenValue = tokenHandler.WriteToken(token); return(tokenValue); }
public Task <UserForRetrievalUiModel> UpdateUserRefreshTokenAsync(Guid refreshToken) { var response = new UserForRetrievalUiModel() { Message = "START_MODIFICATION" }; if (refreshToken == Guid.Empty) { response.Message = "ERROR_INVALID_USER_REFRESH_TOKEN"; return(Task.Run(() => response)); } try { var userToBeModified = _userRepository.FindUserByRefreshTokenAsync(refreshToken); if (userToBeModified == null) { throw new UserDoesNotExistException(refreshToken); } userToBeModified.ModifyWithRefreshToken(refreshToken); userToBeModified.InjectWithUserToken(new UserToken()); Log.Debug( $"Update User with RefreshToken: {refreshToken}" + "--UpdateUserRefreshTokenAsync-- @NotComplete@ [UpdateUserProcessor]. " + "Message: Just Before MakeItPersistence"); MakePatientPersistent(userToBeModified); Log.Debug( $"Update User with RefreshToken: {refreshToken}" + "--UpdateUserRefreshTokenAsync-- @NotComplete@ [UpdateUserProcessor]. " + "Message: Just After MakeItPersistence"); response.Message = "SUCCESS_CREATION"; return(Task.Run(() => _autoMapper.Map <UserForRetrievalUiModel>(userToBeModified))); } catch (UserDoesNotExistException ex) { response.Message = "ERROR_USER_DOES_NOT_EXISTS"; Log.Error( $"Update User with RefreshToken: {refreshToken}" + $"Error Message:{response.Message}" + $"--UpdateUserRefreshTokenAsync-- @fail@ [UpdateUserProcessor]. " + $"@innerfault:{ex.Message} and {ex.InnerException}"); } catch (Exception ex) { response.Message = "UNKNOWN_ERROR"; Log.Error( $"Update User with RefreshToken: {refreshToken}" + $"Error Message:{response.Message}" + $"--UpdateUserRefreshTokenAsync-- @fail@ [UpdateUserProcessor]. " + $"@innerfault:{ex.Message} and {ex.InnerException}"); } return(Task.Run(() => response)); }