您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TokenManager.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.IdentityModel.Tokens;
  3. using System.Security.Claims;
  4. using iiie.Authentication.DBO;
  5. namespace iiie.Authentication.Business.JWT
  6. {
  7. /// <summary>
  8. /// Token creation class
  9. /// </summary>
  10. public static class TokenManager
  11. {
  12. /// <summary>
  13. /// Create signing credentials to sign the token
  14. /// </summary>
  15. /// <returns>The credentials</returns>
  16. private static SigningCredentials CreateSigningCredentials()
  17. {
  18. string symmetricKey = "";//AuthProvider.Instance.GetCredentialKey();
  19. byte[] keybytes = Convert.FromBase64String(symmetricKey);
  20. SecurityKey securityKey = new InMemorySymmetricSecurityKey(keybytes);
  21. SigningCredentials signingCredentials =
  22. new SigningCredentials(securityKey,
  23. SecurityAlgorithms.HmacSha256Signature,
  24. SecurityAlgorithms.Sha256Digest);
  25. return signingCredentials;
  26. }
  27. /// <summary>
  28. /// Create a JWT token
  29. /// </summary>
  30. /// <param name="user">The user</param>
  31. /// <returns>The token</returns>
  32. public static string GetToken(UserDboAuth user, String salt)
  33. {
  34. var stringValidator = "";//AuthProvider.Instance.GetValidatorString();
  35. JwtSecurityToken jst = new JwtSecurityToken("urn:" + stringValidator,
  36. stringValidator,
  37. new Claim[]
  38. {
  39. new Claim(ClaimTypes.Name, user.Username),
  40. new Claim(ClaimTypes.Authentication, salt)
  41. }, null, DateTime.Now.AddDays(1),
  42. CreateSigningCredentials());
  43. JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
  44. return jh.WriteToken(jst);
  45. }
  46. }
  47. }