Esempio n. 1
0
        public bool AddUser(string userName, string password)
        {
            if (CheckIfUserExsit(userName))
            {
                return(false);
            }
            else
            {
                string hashedPass = BcryptHash.HashPassword(password);
                Console.WriteLine(hashedPass + "Here");

                Aes aes = new Aes();

                Console.WriteLine($" key {aes.GetKey()}");
                string cryptPass = aes.EncryptToBase64String(hashedPass);
                Console.WriteLine($" key {aes.GetKey()}");
                Console.WriteLine(aes.DecryptFromBase64String(cryptPass));

                userList.Add(new UserInfo {
                    UserID = userName, Password = cryptPass
                });

                mockDB.SetData(userList);

                return(true);
            }
        }
Esempio n. 2
0
        public bool LogIn(string user, string pass)
        {
            if (FindUser(user))
            {
                Console.WriteLine(hiddenPass);
                Aes aes = new Aes();

                string decryptPass = aes.DecryptFromBase64String(hiddenPass);

                if (BcryptHash.ValidatePassword(pass, decryptPass))
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            return(false);
        }
Esempio n. 3
0
        public async Task ChangePasswordAsync(ChangePasswordDTO model)
        {
            if (string.IsNullOrWhiteSpace(model.CurrentPassword) && string.IsNullOrWhiteSpace(model.NewPassword))
            {
                throw new Exception("Password is empty.");
            }

            var user = await this.m_userRepository.GetByIdAsync(model.Id);

            if (user == null)
            {
                throw new Exception("User not found.");
            }

            if (!BcryptHash.CheckBcryptPassword(model.CurrentPassword, user.Password))
            {
                throw new Exception("Incorrect password.");
            }

            user.Password = await this.EncryptPasswordAsync(model.NewPassword);

            await this.m_userRepository.UpdateAsync(user);
        }
Esempio n. 4
0
 public async Task <string> EncryptPasswordAsync(string pass)
 {
     return(await Task.Run(() => BcryptHash.GenerateBcryptHash(pass)));
 }
Esempio n. 5
0
        private async Task <ClaimsIdentity> GetIdentityAsync(LoginUserDTO model, bool reg)
        {
            var user = new User {
                Role = UserRoles.User
            };

            if (!reg)
            {
                var res = await this.m_userRepository.GetAllAsync(new List <Expression <Func <User, bool> > > {
                    u => u.Login == model.Login
                });

                user = await res.FirstOrDefaultAsync();

                if (user == null)
                {
                    throw new Exception("Login not found.");
                }
                var pass = user.Password;
                reg = await this.m_userRepository.ExistAsync(u => u.Login == model.Login && BcryptHash.CheckBcryptPassword(model.Password, pass));

                if (!reg)
                {
                    throw new Exception("Incorrect password.");
                }
            }

            var claims = new List <Claim> {
                new Claim("Login", model.Login),
                new Claim("Role", user.Role.ToString()),
                new Claim("Id", user.Id.ToString())
            };

            var claimsIdentity = new ClaimsIdentity(claims,
                                                    "TokenAsync",
                                                    ClaimsIdentity.DefaultNameClaimType,
                                                    ClaimsIdentity.DefaultRoleClaimType);

            return(claimsIdentity);
        }