The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PasswordHasher.cs 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Microsoft.AspNetCore.Identity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Teknik.Configuration;
  7. using Microsoft.Extensions.Identity.Core;
  8. using Microsoft.Extensions.Options;
  9. using Teknik.Utilities.Cryptography;
  10. using Teknik.Utilities;
  11. using System.Text;
  12. using Teknik.IdentityServer.Models;
  13. namespace Teknik.IdentityServer.Security
  14. {
  15. public class TeknikPasswordHasher : PasswordHasher<ApplicationUser>
  16. {
  17. private readonly Config _config;
  18. public TeknikPasswordHasher(Config config)
  19. {
  20. _config = config;
  21. }
  22. public override PasswordVerificationResult VerifyHashedPassword(ApplicationUser user, string hashedPassword, string providedPassword)
  23. {
  24. if (hashedPassword == null)
  25. {
  26. throw new ArgumentNullException(nameof(hashedPassword));
  27. }
  28. if (providedPassword == null)
  29. {
  30. throw new ArgumentNullException(nameof(providedPassword));
  31. }
  32. // Test legacy password hashes
  33. #region Legacy Checks
  34. byte[] hashBytes = SHA384.Hash(user.UserName.ToLower(), providedPassword);
  35. string hash = hashBytes.ToHex();
  36. if (hashedPassword == hash)
  37. {
  38. return PasswordVerificationResult.SuccessRehashNeeded;
  39. }
  40. hash = Encoding.ASCII.GetString(hashBytes);
  41. if (hashedPassword == hash)
  42. {
  43. return PasswordVerificationResult.SuccessRehashNeeded;
  44. }
  45. hash = SHA256.Hash(providedPassword, _config.Salt1, _config.Salt2);
  46. if (hashedPassword == hash)
  47. {
  48. return PasswordVerificationResult.SuccessRehashNeeded;
  49. }
  50. #endregion
  51. // Test Latest
  52. return base.VerifyHashedPassword(user, hashedPassword, providedPassword);
  53. }
  54. }
  55. }