コード例 #1
0
        public ActionResult Add(AddUserVM newUser)
        {
            IEnumerable <IdentityRole> roles = roleAppService.GetALL();

            newUser.AllRoles = roles;
            if (ModelState.IsValid == false)
            {
                return(View(newUser));
            }
            string         roleName = Request["Role"];
            IdentityResult result   = userAppService.AddUser(newUser);

            if (result.Succeeded)
            {
                ApplicationUserIdentity identityUser = userAppService.Find(newUser.UserName, newUser.PasswordHash);
                userAppService.AssignToRole(identityUser.Id, roleName);
                TempData["Msg"] = "New User Added Successfully";
                return(RedirectToAction("Index"));
            }
            else
            {
                ModelState.AddModelError("", result.Errors.FirstOrDefault());
                return(View(newUser));
            }
        }
コード例 #2
0
 public ActionResult AddUser(string userAccount, bool isAdmin, string password)
 {
     try
     {
         User user = _userService.AddUser(userAccount, password, isAdmin);
         return(Json("添加成功", JsonRequestBehavior.AllowGet));
     }
     catch
     {
         return(Json("添加失败", JsonRequestBehavior.AllowGet));
     }
 }
コード例 #3
0
 public ActionResult Register(string userAccount, string password)
 {
     try
     {
         User user = _userService.AddUser(userAccount, password, false);
         return(Json("注册成功", JsonRequestBehavior.AllowGet));
     }
     catch
     {
         return(Json("该帐号已注册", JsonRequestBehavior.AllowGet));
     }
 }
コード例 #4
0
        public ActionResult Edit(UserViewModel model)
        {
            model.UserStates = GetUserStates(); //Reload List For Clean Model

            try
            {
                var svc = new UserAppService();

                var o = new User
                {
                    UserName = model.UserName,
                    
                    UserStateId = model.UserStateId,
                    HasChangePassword = model.HasChangePassword


                };
                if (model.UserId == -1)
                {
                    string salt;
                    var hashedPassword = WebHelper.EncryptPassword(model.Password, out salt);
                    o.Password = hashedPassword;
                    o.Salt = salt;
                }
                else
                {
                    if (String.IsNullOrEmpty(model.Password))
                    {
                        var ousr = new UserAppService().GetUser(model.UserId);

                        o.Password = ousr.Password;
                        o.Salt = ousr.Salt;

                    }
                    else
                    { //GetCurrentData
                        string salt;
                        var hashedPassword = WebHelper.EncryptPassword(model.Password, out salt);
                        o.Password = hashedPassword;
                        o.Salt = salt;
                        
                    }

                }


                if (model.UserId == -1)
                {

                    if(CommonHelper.ExistUserName(model.UserName))
                    {
                        ViewBag.Feed = 2;
                        return View(model);

                    }

                    o.LastLoginDate = DateTime.Now;
                    o.AccessCount = 0;
                    o.TryAccessCount = 0;
                   var userId= svc.AddUser(o);

                    var svcUserRole = new UserRoleAppService();

                    var roles = model.AssignedUserRole.Split(',');
                    foreach (var r in roles)
                    {
                        svcUserRole.AddUserRole(new UserRole { RoleId = Convert.ToInt32(r), UserId = userId });

                    }
                    

                    
                    ViewBag.Feed = 0;
                }
                else
                {
                    o.UserId = model.UserId;
                    o.LastLoginDate = DateTime.Now;
                    if (model.IsDeleteAction == 0)
                    {

                        svc.SaveUser(o);


                        var svcUserRole = new UserRoleAppService();
                        svcUserRole.RemoveUserRoleByUserId(o.UserId);
                        var roles = model.AssignedUserRole.Split(',');
                        foreach (var r in roles)
                        {
                            svcUserRole.AddUserRole(new UserRole { RoleId = Convert.ToInt32(r), UserId = o.UserId });

                        }

                        var eusr = new UserAppService().GetUser(model.UserId);
                        model.UserRoles = eusr.UserRoles.ToList();

                    }
                    else
                    {
                        var svcUserRole = new UserRoleAppService();
                        svcUserRole.RemoveUserRoleByUserId(o.UserId);
                        svc.RemoveUser(model.UserId);
                        model.UserRoles = new List<UserRole>();
                    }
                    ViewBag.Feed = 0;
                }
            }
            catch (Exception)
            {
                ViewBag.Feed = 1;

            }
      
            return View(model);
        }