예제 #1
0
 public ActionResult Edit(ApplicationUser model)
 {
     if (ModelState.IsValid)
     {
         var passwordHash = new PasswordHasher();
         if (!string.IsNullOrWhiteSpace(model.NewPassword))
         {
             model.PasswordHash = passwordHash.HashPassword(model.NewPassword);
         }
         _unitOfWork.DataContext.Entry(model).State = EntityState.Modified;
         _unitOfWork.Save();
         return RedirectToAction("LogOff", "Account", new { @area = "" });
     }
     return RedirectToAction("List");
 }
예제 #2
0
 public ActionResult Remove(ApplicationUser model)
 {
     if (ModelState.IsValid)
     {
         _unitOfWork.DataContext.Entry(model).State = EntityState.Deleted;
         _unitOfWork.Save();
         return RedirectToAction("List");
     }
     return RedirectToAction("List");
 }
예제 #3
0
        public async Task<ActionResult> Add(UserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_unitOfWork.DataContext));

                var user = new ApplicationUser()
                {
                    UserName = model.UserName,
                    Email = model.Email,
                    EmailConfirmed = true,
                    FullName = model.FullName
                };

                var roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(_unitOfWork.DataContext));

                var role = await roleManager.FindByNameAsync("roleAdmin");
                var result = await manager.CreateAsync(user, model.PasswordHash);
                if (result.Succeeded && role != null)
                {
                    manager.AddToRole(user.Id, role.Name);
                }
                else
                {
                    throw new NullReferenceException();
                }

                return RedirectToAction("List");
            }
            return HttpNotFound();
        }