Esempio n. 1
0
        public async Task <IActionResult> Edit(string userId, List <string> roles)
        {
            // получаем пользователя
            UserIdent user = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                // получем список ролей пользователя
                var userRoles = await _userManager.GetRolesAsync(user);

                // получаем все роли
                var allRoles = _roleManager.Roles.ToList();
                // получаем список ролей, которые были добавлены
                var addedRoles = roles.Except(userRoles);
                // получаем роли, которые были удалены
                var removedRoles = userRoles.Except(roles);

                await _userManager.AddToRolesAsync(user, addedRoles);

                await _userManager.RemoveFromRolesAsync(user, removedRoles);

                return(RedirectToAction("UserList"));
            }

            return(NotFound());
        }
Esempio n. 2
0
        public async Task <IActionResult> PostAsync([FromBody] RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserIdent user = new UserIdent()
                {
                    Email = model.Email, UserName = model.Email, Year = model.Year
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, false);

                    return(Ok(result));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                }
            }

            return(null);
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserIdent user = await _userManager.FindByIdAsync(model.Id);

                if (user != null)
                {
                    user.Email    = model.Email;
                    user.UserName = model.Email;
                    user.Year     = model.Year;

                    var result = await _userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                    }
                }
            }

            return(View(model));
        }
Esempio n. 4
0
        public async Task <IActionResult> Put(string id, [FromBody] EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserIdent user = await _userManager.FindByIdAsync(id);

                if (user != null)
                {
                    user.Email    = model.Email;
                    user.UserName = model.Email;
                    user.Year     = model.Year;

                    var result = await _userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(Ok(result));
                    }
                    else
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 5
0
        public async Task <IActionResult> Delete(string Id)
        {
            UserIdent user = await _userManager.FindByIdAsync(Id);

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

            return(RedirectToAction("Index"));
        }
Esempio n. 6
0
        public async Task <IActionResult> Edit(string Id)
        {
            UserIdent user = await _userManager.FindByIdAsync(Id);

            if (user == null)
            {
                return(NotFound());
            }

            EditUserViewModel model = new EditUserViewModel {
                Id = user.Id, Email = user.Email, Year = user.Year
            };

            return(View(model));
        }
Esempio n. 7
0
        public async Task <IActionResult> ChangePassword(string id)
        {
            UserIdent user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                return(NotFound());
            }

            ChangePasswordViewModel model = new ChangePasswordViewModel {
                Id = user.Id, Email = user.Email
            };

            return(View(model));
        }
Esempio n. 8
0
        public async Task <IActionResult> Delete(string id)
        {
            UserIdent user = await _userManager.FindByIdAsync(id);

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

                return(Ok());
            }
            else
            {
                return(NotFound("Not found"));
            }
        }
Esempio n. 9
0
        public void Create_user()
        {
            string    username = "******";
            UserIdent user     = new UserIdent(username);

            Assert.IsNotNull(user);
            Assert.IsTrue(user is Identity ident);
            Assert.AreEqual(username, user.Name);
            Assert.IsFalse(user.IsPlan);
            Assert.AreEqual(0, user.Number);
            Assert.AreEqual(user.Name, user.BasicName);
            Assert.IsTrue(user.Matches(username));
            Assert.IsTrue(user.Matches("KINEMATICS"));
            Assert.IsTrue(user.Matches("kinematics"));

            Assert.AreEqual(username, user.ToString());
        }
Esempio n. 10
0
        public async Task <IActionResult> Edit(string userId)
        {
            // получаем пользователя
            UserIdent user = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                // получем список ролей пользователя
                var userRoles = await _userManager.GetRolesAsync(user);

                var allRoles = _roleManager.Roles.ToList();
                ChangeRoleViewModel model = new ChangeRoleViewModel
                {
                    UserId    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };
                return(View(model));
            }

            return(NotFound());
        }
Esempio n. 11
0
        public async Task <IActionResult> Create(CreateUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserIdent user = new UserIdent {
                    Email = model.Email, UserName = model.Email, Year = model.Year
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(String.Empty, error.Description);
                    }
                }
            }
            return(View(model));
        }
Esempio n. 12
0
 public void Create_user_empty()
 {
     UserIdent user = new UserIdent("");
 }
Esempio n. 13
0
 public void Create_user_null()
 {
     UserIdent user = new UserIdent(null);
 }
Esempio n. 14
0
        public async Task <IActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserIdent user = await _userManager.FindByIdAsync(model.Id);

                if (user != null)
                {
                    var _passwordValidator =
                        HttpContext.RequestServices.GetService(typeof(IPasswordValidator <UserIdent>)) as
                        IPasswordValidator <UserIdent>;
                    var _passwordHasher =
                        HttpContext.RequestServices.GetService(typeof(IPasswordHasher <UserIdent>)) as
                        IPasswordHasher <UserIdent>;

                    IdentityResult result =
                        await _passwordValidator.ValidateAsync(_userManager, user, model.NewPassword);

                    if (result.Succeeded)
                    {
                        user.PasswordHash = _passwordHasher.HashPassword(user, model.NewPassword);
                        await _userManager.UpdateAsync(user);

                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Пользователь ненайден");
                }
            }

            return(View(model));

            //второй вариант

            //if (ModelState.IsValid)
            //{
            //    User user = await _userManager.FindByIdAsync(model.Id);
            //    if (user != null)
            //    {
            //        IdentityResult result =
            //            await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);
            //        if (result.Succeeded)
            //        {
            //            return RedirectToAction("Index");
            //        }
            //        else
            //        {
            //            foreach (var error in result.Errors)
            //            {
            //                ModelState.AddModelError(string.Empty, error.Description);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        ModelState.AddModelError(string.Empty, "Пользователь не найден");
            //    }
            //}
            //return View(model);
        }