コード例 #1
0
        public async Task <OperationDetails> Register(DTORegisterViewModel dtoRegisterViewModel)
        {
            ApplicationUser user = await Database.UserManager.FindByEmailAsync(dtoRegisterViewModel.Email);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = dtoRegisterViewModel.Email, Email = dtoRegisterViewModel.Email
                };

                var password         = new UserPasswordValidator();
                var validatePassword = password.ValidateAsync(dtoRegisterViewModel.Password).Result;
                if (validatePassword.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, validatePassword.Errors.FirstOrDefault(), ""));
                }

                var result = await Database.UserManager.CreateAsync(user, dtoRegisterViewModel.Password);

                if (result.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }

                var code = await Database.UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                return(new OperationDetails(true, user.Id, code));
            }
            else
            {
                return(new OperationDetails(false, "Пользователь с таким логином (Email) уже существует", "Email"));
            }
        }
コード例 #2
0
        public string ChangePassword(DTOChangePasswordViewModel dtoChangePasswordViewModel)
        {
            var password         = new UserPasswordValidator();
            var validatePassword = password.ValidateAsync(dtoChangePasswordViewModel.NewPassword).Result;

            if (validatePassword.Errors.Count() > 0)
            {
                return(validatePassword.Errors.First());
            }

            var userCurrent = Database.GetManager(dtoChangePasswordViewModel.Login);

            if (userCurrent == null)
            {
                return("Close this window and reset program.");
            }

            var result = Database.UserManager.ChangePassword(userCurrent.Id, dtoChangePasswordViewModel.OldPassword, dtoChangePasswordViewModel.NewPassword);

            if (result.Succeeded)
            {
                return(null);
            }
            return(result.Errors.First());
        }
コード例 #3
0
 public UserService(IMapper mapper, ICompanyRepository repository, UserManager <User> userManager, IHttpContextAccessor httpContextAccessor, ICacheService cache)
 {
     _userManager         = userManager;
     _companyRepository   = repository;
     _httpContextAccessor = httpContextAccessor;
     _cache = cache;
     _createValidationRules   = new CreateUserValidator();
     _passwordValidationRules = new UserPasswordValidator();
     _mapper           = mapper;
     _userListCacheKey = "userList";
 }
コード例 #4
0
        public void ValidateTest()
        {
            UserPasswordValidator validator = new UserPasswordValidator();
            string error = validator.Validate(new User(), null);

            Assert.IsFalse(string.IsNullOrEmpty(error));
            error = validator.Validate(new User(), "");
            Assert.IsFalse(string.IsNullOrEmpty(error));
            error = validator.Validate(new User(), "test");
            Assert.IsTrue(string.IsNullOrEmpty(error));
        }
コード例 #5
0
        public async Task <OperationDetails> ChangePassword(DTOChangePasswordViewModel changePasswordViewModelDto)
        {
            var password         = new UserPasswordValidator();
            var validatePassword = password.ValidateAsync(changePasswordViewModelDto.NewPassword).Result;

            if (validatePassword.Errors.Count() > 0)
            {
                return(new OperationDetails(false, validatePassword.Errors.FirstOrDefault(), ""));
            }

            var result = await Database.UserManager.ChangePasswordAsync(changePasswordViewModelDto.UserId, changePasswordViewModelDto.OldPassword,
                                                                        changePasswordViewModelDto.NewPassword);

            if (result.Succeeded)
            {
                return(new OperationDetails(true, "", ""));
            }
            return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
        }
コード例 #6
0
        public async Task <OperationDetails> ResetPasswordAsync(string userId, string code, string _password)
        {
            var password         = new UserPasswordValidator();
            var validatePassword = password.ValidateAsync(_password).Result;

            if (validatePassword.Errors.Count() > 0)
            {
                return(new OperationDetails(false, validatePassword.Errors.FirstOrDefault(), ""));
            }

            var result = await Database.UserManager.ResetPasswordAsync(userId, code, _password);

            if (result.Errors.Count() > 0)
            {
                return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
            }
            else
            {
                return(new OperationDetails(true, "", ""));
            }
        }
コード例 #7
0
ファイル: UserService.cs プロジェクト: Yuriy1986/ForumNew
        public async Task <OperationDetails> Register(DTORegisterViewModel dtoRegisterViewModel)
        {
            ApplicationUser user = await Database.UserManager.FindByEmailAsync(dtoRegisterViewModel.Email);

            if (user == null)
            {
                // Check NickName for unique.
                if (Database.UserManager.Users.FirstOrDefault(x => x.NickName == dtoRegisterViewModel.NickName) != null)
                {
                    return(new OperationDetails(false, "User with the same NickName already exists.", "NickName"));
                }

                user = new ApplicationUser {
                    UserName = dtoRegisterViewModel.Email, Email = dtoRegisterViewModel.Email, NickName = dtoRegisterViewModel.NickName, Online = true, TimeLogin = DateTime.Now
                };

                var password         = new UserPasswordValidator();
                var validatePassword = password.ValidateAsync(dtoRegisterViewModel.Password).Result;
                if (validatePassword.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, validatePassword.Errors.FirstOrDefault(), ""));
                }

                var result = await Database.UserManager.CreateAsync(user, dtoRegisterViewModel.Password);

                if (result.Errors.Count() > 0)
                {
                    return(new OperationDetails(false, result.Errors.FirstOrDefault(), ""));
                }
                await Database.UserManager.AddToRoleAsync(user.Id, "user");

                var code = await Database.UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                return(new OperationDetails(true, user.Id, code));
            }
            else
            {
                return(new OperationDetails(false, "User with the same Login (Email) already exists.", "Email"));
            }
        }