Пример #1
0
        public async Task <IActionResult> Create(CreateModel model)
        {
            if (ModelState.IsValid)
            {
                MDSUser user = new MDSUser
                {
                    UserName    = model.Name,
                    Email       = model.Email,
                    PhoneNumber = model.PhoneNumber
                };
                IdentityResult result
                    = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    foreach (IdentityError error in result.Errors)
                    {
                        AddErrorsFromResult(result);
                    }
                }
            }

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> Edit(string id, string email, string phonenumber,
                                               string password)
        {
            MDSUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                user.Email       = email;
                user.PhoneNumber = phonenumber;
                IdentityResult validEmail
                    = await userValidator.ValidateAsync(userManager, user);

                if (!validEmail.Succeeded)
                {
                    AddErrorsFromResult(validEmail);
                }
                IdentityResult validPass = null;
                if (!string.IsNullOrEmpty(password))
                {
                    validPass = await passwordValidator.ValidateAsync(userManager,
                                                                      user, password);

                    if (validPass.Succeeded)
                    {
                        user.PasswordHash = passwordHasher.HashPassword(user,
                                                                        password);
                    }
                    else
                    {
                        AddErrorsFromResult(validPass);
                    }
                }
                if ((validEmail.Succeeded && validPass == null) ||
                    (validEmail.Succeeded &&
                     password != string.Empty && validPass.Succeeded))
                {
                    IdentityResult result = await userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        AddErrorsFromResult(result);
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Пользователь не найден");
            }
            return(View(user));
        }
Пример #3
0
        public async Task <IActionResult> Edit(string id)
        {
            MDSUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                return(View(user));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
Пример #4
0
        public async Task <IActionResult> Edit(RoleModificationModel model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.IdsToAdd ?? new string[] { })
                {
                    MDSUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user,
                                                                  model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
                foreach (string userId in model.IdsToDelete ?? new string[] { })
                {
                    MDSUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user,
                                                                       model.RoleName);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await Edit(model.RoleId));
            }
        }
Пример #5
0
        public async Task <IActionResult> Delete(string id)
        {
            MDSUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                IdentityResult result = await userManager.DeleteAsync(user);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            else
            {
                ModelState.AddModelError("", "Пользователь не найден");
            }
            return(View("Index", userManager.Users));
        }