Beispiel #1
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);
        }
Beispiel #2
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);
        }