示例#1
0
        public ActionResult Create(RegisterModel model)
        {
            ViewBag.Des = "新增操作员";
            var roles = _service.GetAllUserRoles().TakeWhile(n => n.SystemName != SystemUserRoleNames.Administrators);
            ViewBag.Roles = new SelectList(roles, "Id", "Name");
            User newuser;
            if (model.Id == 0)
            {
                var user = new User
                {
                    UserGuid = Guid.NewGuid(),
                    Username = model.UserName,
                    Email = model.Email,
                    Mobile = model.Mobile,
                    Active = true,
                    //加密存储
                    Password = Encrypt.EncryptString(model.Password),
                };
                //默认增加注册角色
                // 先插入
                _service.InsertUser(user);
                newuser = _service.GetUserByUsername(user.Username);
            }
            else
            {
                newuser = _service.GetUserById(model.Id);
                newuser.Username = model.UserName;
                newuser.Password = Encrypt.EncryptString(model.Password);
                newuser.Email = model.Email;
                newuser.Mobile = model.Mobile;
                ViewBag.Des = "编辑用户";
            }

            var role = _service.GetUserRoleById(model.RoleId);
            //先只有一个角色
            newuser.UserRoles.Clear();
            newuser.UserRoles.Add(role);

            try
            {
                _service.UpdateUser(newuser);
                Success();
                model.Empty();
            }
            catch (Exception e)
            {
                Error(e.Message);
            }
            return View(model);
        }
示例#2
0
        public ActionResult Create(int id = 0)
        {
            ViewBag.Des = "新增操作员";
            RegisterModel user;
            if (id == 0)
            {
               user = new RegisterModel(id);
            }
            else
            {
                var rawuser = _service.GetUserById(id);
                if (rawuser == null) return View("NoData");
                ViewBag.Des = "编辑用户";
                user=new RegisterModel(rawuser);
                Session["NameKey"] = rawuser.Username;
            }

            var roles = _service.GetAllUserRoles().TakeWhile(n=>n.SystemName!=SystemUserRoleNames.Administrators);
            ViewBag.Roles = new SelectList(roles, "Id", "Name"); 

            return View(user);
        }
示例#3
0
        public ActionResult Register(RegisterModel model, string returnUrl)
        {
            //如果当前用户再注册别的用户,就让他先退出,加入一个Guest角色用户进来准备。
            var user = _service.InsertGuestUser();

            if (ModelState.IsValid)
            {
                if (model.UserName != null)
                {
                    model.UserName = model.UserName.Trim();
                }

                var isApprove = true;
                var registerRequest = new UserRegistrationRequest(user, model.Email, model.Mobile, model.UserName, model.Password, PasswordFormat.Encrypted, isApprove);
                var registrationResult = _accountService.RegisterUser(registerRequest);
                if (registrationResult.Success)
                {
                    if (isApprove)
                    {
                        AuthenticationService.SignIn(user, true);
                    }
                    if (String.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
                        return RedirectToAction("Index", "Home");
                    return Redirect(returnUrl);
                }
                foreach (var error in registrationResult.Errors)
                {
                    ModelState.AddModelError("", error);
                }

            }
            return View(model);
        }
示例#4
0
 public ActionResult Register()
 {
     var model = new RegisterModel();
     return View(model);
 }