Example #1
0
        public ActionResult DeleteOneUser(string Id)
        {
            using (var um = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()))) {
                var user = um.FindById(Id);


                var fu = new FormatedUser {
                    Id           = user.Id,
                    UserName     = user.UserName,
                    PasswordHash = user.PasswordHash,
                    Admin        = um.IsInRole(user.Id, "Admin")
                };

                return(View(fu));
            }
        }
Example #2
0
        public ActionResult EditOneUser(FormatedUser u)
        {
            if (u.UserName == null || u.UserName.Equals(""))
            {
                ModelState.AddModelError(nameof(u.UserName),
                                         "User name cannot be null");
            }
            if (u.PasswordHash == null || u.PasswordHash.Equals(""))
            {
                ModelState.AddModelError(nameof(u.PasswordHash),
                                         "Password cannot be null");
            }
            if (ModelState.IsValid)
            {
                using (var um = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()))) {
                    var user = um.FindById(u.Id);
                    user.UserName = u.UserName;
                    user.Email    = u.UserName;
                    um.Update(user);
                    if (!user.PasswordHash.Equals(u.PasswordHash))
                    {
                        um.RemovePassword(u.Id);

                        um.AddPassword(u.Id, u.PasswordHash);
                    }
                    if (u.Admin)
                    {
                        if (!um.IsInRole(u.Id, "Admin"))
                        {
                            um.AddToRole(u.Id, "Admin");
                        }
                    }
                    else
                    {
                        if (um.IsInRole(u.Id, "Admin"))
                        {
                            um.RemoveFromRole(u.Id, "Admin");
                        }
                    }

                    return(RedirectToAction("Users"));
                }
            }
            return(View(u));
        }
Example #3
0
 public ActionResult Users()
 {
     using (var um = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()))) {
         var users                 = um.Users.ToList();
         var currentUserId         = System.Web.HttpContext.Current.User.Identity.GetUserId();
         List <FormatedUser> model = new List <FormatedUser>();
         users.ForEach(u => {
             var fu = new FormatedUser {
                 Id           = u.Id,
                 UserName     = u.UserName,
                 PasswordHash = u.PasswordHash,
                 Admin        = um.IsInRole(u.Id, "Admin")
             };
             if (!currentUserId.Equals(u.Id))
             {
                 model.Add(fu);
             }
         });
         return(View(model));
     }
 }