Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
  2. {
  3. public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
  4. {
  5. context.Validated();
  6. }
  7. public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  8. {
  9. context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
  10. bool isLogin = false;
  11. using (DAL.HesabgarClubEntities db = new HesabgarClubEntities())
  12. {
  13. string hashpass = hashMaker.HashPassword(context.Password);
  14. isLogin= db.AspNetUsers.Any(m => m.UserName == context.UserName && m.PasswordHash.Equals(hashpass));
  15. if (!isLogin)
  16. {
  17. context.SetError("invalid_grant", "The user name or password is incorrect." + hashpass);
  18. return;
  19. }
  20. }
  21. var identity = new ClaimsIdentity(context.Options.AuthenticationType);
  22. identity.AddClaim(new Claim("sub", context.UserName));
  23. identity.AddClaim(new Claim("role", "user"));
  24. context.Validated(identity);
  25. }
  26. }
  27.  
  28. public class hashMaker
  29. {
  30. public static bool VerifyHashedPassword(string hashedPassword, string password)
  31. {
  32. byte[] buffer4;
  33. if (hashedPassword == null)
  34. {
  35. return false;
  36. }
  37. if (password == null)
  38. {
  39. throw new ArgumentNullException("password");
  40. }
  41. byte[] src = Convert.FromBase64String(hashedPassword);
  42. if ((src.Length != 0x31) || (src[0] != 0))
  43. {
  44. return false;
  45. }
  46. byte[] dst = new byte[0x10];
  47. Buffer.BlockCopy(src, 1, dst, 0, 0x10);
  48. byte[] buffer3 = new byte[0x20];
  49. Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
  50. using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
  51. {
  52. buffer4 = bytes.GetBytes(0x20);
  53. }
  54. return ByteArrayCompare(buffer3, buffer4);
  55. }
  56.  
  57. static bool ByteArrayCompare(byte[] a1, byte[] a2)
  58. {
  59. return StructuralComparisons.StructuralEqualityComparer.Equals(a1, a2);
  60. }
  61. public static string HashPassword(string password)
  62. {
  63. byte[] salt;
  64. byte[] buffer2;
  65. if (password == null)
  66. {
  67. throw new ArgumentNullException("password");
  68. }
  69. using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8))
  70. {
  71. salt = bytes.Salt;
  72. buffer2 = bytes.GetBytes(0x20);
  73. }
  74. byte[] dst = new byte[0x31];
  75. Buffer.BlockCopy(salt, 0, dst, 1, 0x10);
  76. Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20);
  77. return Convert.ToBase64String(dst);
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement