Guest User

Untitled

a guest
Jun 2nd, 2020
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static class JwtOptions
  2. {
  3. public const string Issuer = "EvaSys Issuer"; // издатель токенов
  4. public const string Audience = "EvaSys Client"; // потребитель токена
  5. const string Key = "1qwedsaf894DFskg0564";
  6.  
  7.  
  8. public static SymmetricSecurityKey GetSymmetricSecurityKey()
  9. {
  10. return new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Key));
  11. }
  12. }
  13. ///////////////
  14.  
  15.  
  16.  
  17. private AuthResultModel GenerateAuthResultForUser(UserModel userModel)
  18. {
  19. var key = JwtOptions.GetSymmetricSecurityKey();
  20. var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
  21.  
  22. var tokenHandler = new JwtSecurityTokenHandler();
  23. var tokenDescriptor = new SecurityTokenDescriptor
  24. {
  25. Subject = new ClaimsIdentity(new[]
  26. {
  27. new Claim("UserName", userModel.UserName),
  28. new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
  29. new Claim(JwtRegisteredClaimNames.Email, userModel.Email),
  30. new Claim("Role", userModel.Role),
  31. new Claim("id", userModel.Id),
  32. }),
  33. Expires = DateTime.UtcNow.AddHours(2),
  34.  
  35. SigningCredentials = signingCredentials
  36. };
  37.  
  38. var token = tokenHandler.CreateToken(tokenDescriptor);
  39.  
  40.  
  41. return new AuthResultModel
  42. {
  43. Token = tokenHandler.WriteToken(token),
  44. Success = true,
  45. UserFirstName = userModel.FirstName,
  46. UserSecondName = userModel.SecondName,
  47. UserMiddleName = userModel.MiddleName,
  48. AvatarImage = userModel.AvatarImage,
  49. UserRole = userModel.Role,
  50. Username = userModel.UserName,
  51. };
  52.  
  53. }
RAW Paste Data