Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TokenManager.cs 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Configuration;
  3. using System.IdentityModel.Tokens;
  4. using System.Security.Claims;
  5. using System.ServiceModel.Security.Tokens;
  6. using Newtonsoft.Json;
  7. namespace iiie.Authentication.Business.JWT
  8. {
  9. /// <summary>
  10. /// Token creation class
  11. /// </summary>
  12. public static class TokenManager
  13. {
  14. /// <summary>
  15. /// Create signing credentials to sign the token
  16. /// </summary>
  17. /// <returns>The credentials</returns>
  18. private static SigningCredentials CreateSigningCredentials()
  19. {
  20. string symmetricKey = ConfigurationManager.AppSettings["CredentialKey"];
  21. byte[] keybytes = Convert.FromBase64String(symmetricKey);
  22. SecurityKey securityKey = new InMemorySymmetricSecurityKey(keybytes);
  23. SigningCredentials signingCredentials =
  24. new SigningCredentials(securityKey,
  25. SecurityAlgorithms.HmacSha256Signature,
  26. SecurityAlgorithms.Sha256Digest);
  27. return signingCredentials;
  28. }
  29. /// <summary>
  30. /// Create a JWT token
  31. /// </summary>
  32. /// <param name="username">The user username</param>
  33. /// <param name="salt">The user salt</param>
  34. /// <param name="data">Additionnal user data</param>
  35. /// <returns>The token</returns>
  36. public static string GetToken(string username, string salt, object data = null)
  37. {
  38. var stringValidator = ConfigurationManager.AppSettings["StringValidator"];
  39. JwtSecurityToken jst = new JwtSecurityToken("urn:" + stringValidator,
  40. stringValidator,
  41. new []
  42. {
  43. new Claim(ClaimTypes.Name, username),
  44. new Claim(ClaimTypes.Authentication, salt),
  45. new Claim(ClaimTypes.UserData, JsonConvert.SerializeObject(data))
  46. }, null, DateTime.Now.AddDays(1),
  47. CreateSigningCredentials());
  48. JwtSecurityTokenHandler jh = new JwtSecurityTokenHandler();
  49. return jh.WriteToken(jst);
  50. }
  51. public static ClaimsPrincipal ParseToken(string token)
  52. {
  53. JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler
  54. {
  55. Configuration = new SecurityTokenHandlerConfiguration()
  56. {
  57. MaxClockSkew = new TimeSpan(0, 1, 0)
  58. }
  59. };
  60. var stringValidator = ConfigurationManager.AppSettings["StringValidator"];
  61. TokenValidationParameters validationParameters = new TokenValidationParameters()
  62. {
  63. RequireSignedTokens = true,
  64. RequireExpirationTime = true,
  65. ValidAudience = stringValidator,
  66. ValidateIssuerSigningKey = true,
  67. ValidIssuer = "urn:" + stringValidator,
  68. IssuerSigningToken = new BinarySecretSecurityToken(Convert.FromBase64String(ConfigurationManager.AppSettings["CredentialKey"]))
  69. };
  70. SecurityToken validateToken;
  71. return tokenHandler.ValidateToken(token, validationParameters, out validateToken);
  72. }
  73. }
  74. }