예제 #1
0
        public async Task <ActionResult> Edit(EditUserModel model)
        {
            if (ModelState.IsValid)
            {
                ICSUser user = await UserManager.FindByIdAsync(model.Id);

                if (user != null)
                {
                    user.Email       = model.Email;
                    user.FIO         = model.FIO;
                    user.Year        = model.Year;
                    user.PhoneNumber = model.PhoneNumber;
                    IdentityResult result = await UserManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Что-то пошло не так");
                    }
                }
            }
            return(View(model));
        }
예제 #2
0
        public async Task <ActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                ICSRole role = await RoleManager.FindByNameAsync("user");

                ICSUser user = new ICSUser {
                    FIO         = model.FIO,
                    UserName    = model.Email,
                    Email       = model.Email,
                    Year        = DateTime.Now.Year,
                    RoleId      = role.Id,
                    PhoneNumber = model.PhoneNumber
                };
                IdentityResult result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await UserManager.UpdateAsync(user);

                    await UserManager.AddToRoleAsync(user.Id, "user");

                    return(RedirectToAction("Login", "Users"));
                }
                else
                {
                    foreach (string error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }
            return(View(model));
        }
예제 #3
0
        public async Task <ActionResult> Change(EditUserModel model)
        {
            List <ICSRole> roles = RoleManager.Roles.ToList();

            model.AllRoles = new SelectList(roles, "Id", "Description", model.RoleId);

            if (ModelState.IsValid)
            {
                ICSUser user = await UserManager.FindByIdAsync(model.Id);

                ICSRole role = await RoleManager.FindByIdAsync(user.RoleId);

                if (user != null)
                {
                    await UserManager.RemoveFromRoleAsync(user.Id, role.Name);

                    role = await RoleManager.FindByIdAsync(model.RoleId);

                    await UserManager.AddToRoleAsync(user.Id, role.Name);

                    user.RoleId = model.RoleId;
                    IdentityResult result = await UserManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Что-то пошло не так");
                    }
                }
            }
            return(View(model));
        }
예제 #4
0
        public async Task <ActionResult> ResetPassword(string id)
        {
            ICSUser user = await UserManager.FindByIdAsync(id);

            return(View(new ResetPasswordViewModel {
                Id = id, Email = user.Email
            }));
        }
예제 #5
0
        public async Task <ActionResult> Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                bool UserExist = false;

                ICSUser user = await UserManager.FindAsync(model.FioOrEmail, model.Password); //ищем пользователя по имени

                if (user == null)
                {
                    ICSUser userEmail = await UserManager.FindByEmailAsync(model.FioOrEmail); //ищем  пользователя по Email (если был введен Email)

                    if (userEmail != null)
                    {
                        user = await UserManager.FindAsync(userEmail.UserName, model.Password);

                        if (user == null)
                        {
                            ModelState.AddModelError("", "Неверный логин или пароль.");
                        }
                        else
                        {
                            UserExist = true;
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Неверный логин или пароль.");
                    }
                }
                else
                {
                    UserExist = true;
                }
                if (UserExist == true)
                {
                    ClaimsIdentity claim = await UserManager.CreateIdentityAsync(user,
                                                                                 DefaultAuthenticationTypes.ApplicationCookie);

                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);
                    if (String.IsNullOrEmpty(returnUrl))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    return(Redirect(returnUrl));
                }
            }
            ViewBag.returnUrl = returnUrl;
            return(View(model));
        }
예제 #6
0
        public async Task <ActionResult> Change(string id)
        {
            List <ICSRole> roles = RoleManager.Roles.ToList();
            ICSUser        user  = await UserManager.FindByIdAsync(id);

            if (user != null)
            {
                return(View(new EditUserModel
                {
                    Email = user.Email,
                    FIO = user.FIO,
                    Year = user.Year,
                    Id = user.Id,
                    RoleId = user.RoleId,
                    PhoneNumber = user.PhoneNumber,
                    Role = user.Role.Description,
                    AllRoles = new SelectList(roles, "Id", "Description", user.RoleId)
                }));
            }
            return(RedirectToAction("Index"));
        }
예제 #7
0
        public async Task <ActionResult> Edit(string id)
        {
            var curUser = UserManager.FindById(User.Identity.GetUserId());

            List <ICSRole> roles = RoleManager.Roles.ToList();
            ICSUser        user  = await UserManager.Users.Include(u => u.Role).FirstAsync(u => u.Id == curUser.Id);

            if (user != null)
            {
                return(View(new EditUserModel
                {
                    Email = user.Email,
                    FIO = user.FIO,
                    Year = user.Year,
                    Id = user.Id,
                    RoleId = user.RoleId,
                    PhoneNumber = user.PhoneNumber,
                    Role = user.Role.Description,
                    AllRoles = new SelectList(roles, "Id", "Description", user.RoleId)
                }));
            }
            return(RedirectToAction("Index"));
        }