Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
- {
- public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
- {
- context.Validated();
- }
- public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
- {
- context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
- bool isLogin = false;
- using (DAL.HesabgarClubEntities db = new HesabgarClubEntities())
- {
- string hashpass = hashMaker.HashPassword(context.Password);
- isLogin= db.AspNetUsers.Any(m => m.UserName == context.UserName && m.PasswordHash.Equals(hashpass));
- if (!isLogin)
- {
- context.SetError("invalid_grant", "The user name or password is incorrect." + hashpass);
- return;
- }
- }
- var identity = new ClaimsIdentity(context.Options.AuthenticationType);
- identity.AddClaim(new Claim("sub", context.UserName));
- identity.AddClaim(new Claim("role", "user"));
- context.Validated(identity);
- }
- }
- public class hashMaker
- {
- public static bool VerifyHashedPassword(string hashedPassword, string password)
- {
- byte[] buffer4;
- if (hashedPassword == null)
- {
- return false;
- }
- if (password == null)
- {
- throw new ArgumentNullException("password");
- }
- byte[] src = Convert.FromBase64String(hashedPassword);
- if ((src.Length != 0x31) || (src[0] != 0))
- {
- return false;
- }
- byte[] dst = new byte[0x10];
- Buffer.BlockCopy(src, 1, dst, 0, 0x10);
- byte[] buffer3 = new byte[0x20];
- Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
- using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
- {
- buffer4 = bytes.GetBytes(0x20);
- }
- return ByteArrayCompare(buffer3, buffer4);
- }
- static bool ByteArrayCompare(byte[] a1, byte[] a2)
- {
- return StructuralComparisons.StructuralEqualityComparer.Equals(a1, a2);
- }
- public static string HashPassword(string password)
- {
- byte[] salt;
- byte[] buffer2;
- if (password == null)
- {
- throw new ArgumentNullException("password");
- }
- using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8))
- {
- salt = bytes.Salt;
- buffer2 = bytes.GetBytes(0x20);
- }
- byte[] dst = new byte[0x31];
- Buffer.BlockCopy(salt, 0, dst, 1, 0x10);
- Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20);
- return Convert.ToBase64String(dst);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement