Exemplo n.º 1
0
        public async Task <ResetPasswordResultViewModel> SetPassword(SetPasswordViewModel setPassword, UserManager <Models.User> userManager, SignInManager <Models.User> signInManager)
        {
            ResetPasswordResultViewModel result = new ResetPasswordResultViewModel();

            try
            {
                //var user = (from u in _dbContext.Users
                //            where u.PhoneNumber == setPassword.Phone
                //            select u).FirstOrDefault();

                var user = (from u in userManager.Users
                            where u.PhoneNumber == setPassword.Phone
                            select u).FirstOrDefault();

                await userManager.RemovePasswordAsync(user);

                IdentityResult identityResult = await userManager.AddPasswordAsync(await userManager.FindByIdAsync(user.Id), setPassword.Password);

                //user.PasswordHash = Classes.SimpleHash.ComputeHash(setPassword.Password, "sha256", null);
                //_dbContext.Update(user);
                //_dbContext.SaveChangesAsync();

                result.Id = user.Id.ToString();
                if (user.Avatar == null)
                {
                    result.Avatar = "/dist/img/avatar5.png";
                }
                else
                {
                    result.Avatar = "/uploads/" + user.Id + "/" + user.Avatar;
                }
                result.Message = "success";
                result.Role    = (await userManager.GetRolesAsync(user)).First() ?? "";
                if (user.FullName == null)
                {
                    result.FullName = "نام و نام خانوادگی";
                }
                else
                {
                    result.FullName = user.FullName;
                }
                await signInManager.SignInAsync(user, false);

                return(result);
            }
            catch
            {
                result.Message = "fail";
                return(result);
            }
        }
        public ResetPasswordResultViewModel SetPassword(SetPasswordViewModel setPassword)
        {
            ResetPasswordResultViewModel result = new ResetPasswordResultViewModel();

            try
            {
                var user = (from u in _dbContext.Users
                            where u.PhoneNumber == setPassword.Phone
                            select u).FirstOrDefault();
                user.Password = Classes.SimpleHash.ComputeHash(setPassword.Password, "sha256", null);
                _dbContext.Update(user);
                _dbContext.SaveChangesAsync();

                result.Id = user.Id.ToString();
                if (user.Avatar == null)
                {
                    result.Avatar = "/dist/img/avatar5.png";
                }
                else
                {
                    result.Avatar = "/uploads/" + user.Id + "/" + user.Avatar;
                }
                result.Message = "success";
                result.Role    = user.Role;
                if (user.FullName == null)
                {
                    result.FullName = "نام و نام خانوادگی";
                }
                else
                {
                    result.FullName = user.FullName;
                }
                return(result);
            }
            catch
            {
                result.Message = "fail";
                return(result);
            }
        }