コード例 #1
0
        public Boolean delete()
        {
            //delete from user table
            String deleteUserID = Request.Params["id"];
            int id = int.Parse(deleteUserID);
            IRepository<User> repository = new UserRepository();
            User deletedUser = repository.findById(id);
            if (deletedUser == null)
            {
                return false;
            }
            repository.Delete(deletedUser);

            //delete from asp.netUser table
            var context = new NetCafeWeb.Models.ApplicationDbContext();
            var UserManager = new UserManager<NetCafeWeb.Models.ApplicationUser>(new UserStore<NetCafeWeb.Models.ApplicationUser>(context));
            ApplicationUser aspSelectedUser = context.Users.Where(u => u.UserName.Equals((deletedUser.UserName), StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

            UserManager.DeleteAsync(aspSelectedUser);

            return true;
        }
コード例 #2
0
ファイル: UserService.cs プロジェクト: AnTTSE61258/NetCafeWeb
        public bool updateUser(User selectedUser)
        {
            //Danger: selected user with new role inside
            if(selectedUser != null)
            {
                IRepository<User> repository = new UserRepository();

                //update in aspUser
                User orginUser = repository.findById(selectedUser.UserID);
                //find old role
                Role oldRole = role_repo.findById(orginUser.RoleID);
                string oldRoleName = oldRole.RoleName;
                Role newRole = role_repo.findById(selectedUser.RoleID);
                string newRoleName = newRole.RoleName;
                var context = new NetCafeWeb.Models.ApplicationDbContext();
                var UserManager = new UserManager<NetCafeWeb.Models.ApplicationUser>(new UserStore<NetCafeWeb.Models.ApplicationUser>(context));
                ApplicationUser aspSelectedUser = context.Users.Where(u => u.UserName.Equals((selectedUser.UserName), StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

                var r1 = UserManager.RemoveFromRole(aspSelectedUser.Id, oldRoleName);
                var r2 = UserManager.AddToRole(aspSelectedUser.Id, newRoleName);

                if (r1.Succeeded && r2.Succeeded)
                {
                    //update in user table
                    repository.Update(selectedUser);
                    return true;
                }
                else return false;
            }

            return false;
        }