Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public UserRegistrationResult RegisterUser(UserRegistrationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (request.User == null)
            {
                throw new ArgumentException("当前用户为空");
            }

            var result = new UserRegistrationResult();

            if (request.User.IsRegistered())
            {
                result.AddError("当前用户已经注册");
                return(result);
            }
            if (String.IsNullOrEmpty(request.Email))
            {
                result.AddError("邮箱不能为空");
                return(result);
            }
            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError("邮件格式错误");
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("密码不能为空");
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.Mobile))
            {
                result.AddError("手机号码不能为空");
                return(result);
            }
            if (_userService.GetUserByUsername(request.Username) != null)
            {
                result.AddError("用户名已经存在");
                return(result);
            }

            request.User.Username       = request.Username;
            request.User.Email          = request.Email;
            request.User.PasswordFormat = request.PasswordFormat;
            request.User.Mobile         = request.Mobile;
            request.User.ImgUrl         = "/Content/user_img.jpg";
            switch (request.PasswordFormat)
            {
            case PasswordFormat.Clear:
            {
                request.User.Password = request.Password;
            }
            break;

            case PasswordFormat.Encrypted:
            {
                request.User.Password = Encrypt.GetMd5Code(request.Password);
            }
            break;

            case PasswordFormat.Hashed:
            {
                string saltKey = Encrypt.CreateSaltKey(5);
                request.User.PasswordSalt = saltKey;
                request.User.Password     = Encrypt.CreatePasswordHash(request.Password, saltKey);
            }
            break;

            default:
                break;
            }
            request.User.Active = request.IsApproved;

            // 添加基本角色。
            //var registeredRole = _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered);
            //if (registeredRole == null)
            //    throw new PortalException("'Registered' 角色加载失败");
            if (request.User.Id == 0)
            {
                _userService.InsertUser(request.User);
                request.User = _userService.GetUserByUsername(request.Username);
            }
            //request.User.UserRoles.Add(registeredRole);
            //_userService.UpdateUser(request.User);

            return(result);
        }
        public UserRegistrationResult RegisterUser(UserRegistrationRequest request, bool isMobile = false)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (request.User == null)
            {
                throw new ArgumentException("当前用户为空");
            }

            var result = new UserRegistrationResult();

            if (request.User.IsRegistered())
            {
                result.AddError("当前用户已经注册");
                return(result);
            }
            if (String.IsNullOrEmpty(request.Email) && !isMobile)
            {
                result.AddError("邮箱不能为空");
                return(result);
            }
            if (!CommonHelper.IsValidEmail(request.Email) && !isMobile)
            {
                result.AddError("邮件格式错误");
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("密码不能为空");
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.Mobile))
            {
                result.AddError("手机号码不能为空");
                return(result);
            }
            if (_userService.GetUserByUsername(request.Username) != null && !isMobile)
            {
                result.AddError("用户名已经存在");
                return(result);
            }

            //手机注册更新用户 而不是新增用户

            if (isMobile)
            {
                request.User = _userService.GetUserByMobile(request.SchoolNumber) ?? new User();
            }
            request.User.Username       = request.Username;
            request.User.Email          = request.Email;
            request.User.PasswordFormat = request.PasswordFormat;
            request.User.Mobile         = request.Mobile;
            request.User.Gender         = request.Gender;
            request.User.ImgUrl         = "/Content/user_img.jpg";
            request.User.Password       = Encrypt.GetMd5Code(request.Password);
            request.User.Active         = request.IsApproved;

            // 添加基本角色。
            //var registeredRole = _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered);
            //if (registeredRole == null)
            //    throw new PortalException("'Registered' 角色加载失败");



            if (request.User.Id == 0)
            {
                _userService.InsertUser(request.User);
                request.User = _userService.GetUserByUsername(request.Username);
            }
            else
            {
                _userService.UpdateUser(request.User);
            }
            //request.User.UserRoles.Add(registeredRole);
            //_userService.UpdateUser(request.User);
            return(result);
        }
Exemplo n.º 4
0
       public UserRegistrationResult RegisterUser(UserRegistrationRequest request)
       {
           if (request == null)
               throw new ArgumentNullException("request");

           if (request.User == null)
               throw new ArgumentException("当前用户为空");

           var result = new UserRegistrationResult();

           if (request.User.IsRegistered())
           {
               result.AddError("当前用户已经注册");
               return result;
           }
           if (String.IsNullOrEmpty(request.Email))
           {
               result.AddError("邮箱不能为空");
               return result;
           }
           if (!CommonHelper.IsValidEmail(request.Email))
           {
               result.AddError("邮件格式错误");
               return result;
           }
           if (String.IsNullOrWhiteSpace(request.Password))
           {
               result.AddError("密码不能为空");
               return result;
           }
           if (String.IsNullOrWhiteSpace(request.Mobile))
           {
               result.AddError("手机号码不能为空");
               return result;
           }
           if (_userService.GetUserByUsername(request.Username) != null)
           {
               result.AddError("用户名已经存在");
               return result;
           }

           request.User.Username = request.Username;
           request.User.Email = request.Email;
           request.User.PasswordFormat = request.PasswordFormat;
           request.User.Mobile = request.Mobile;
           request.User.ImgUrl = "/Content/user_img.jpg";
           switch (request.PasswordFormat)
           {
               case PasswordFormat.Clear:
                   {
                       request.User.Password = request.Password;
                   }
                   break;
               case PasswordFormat.Encrypted:
                   {
                       request.User.Password = Encrypt.GetMd5Code(request.Password);
                   }
                   break;
               case PasswordFormat.Hashed:
                   {
                       string saltKey = Encrypt.CreateSaltKey(5);
                       request.User.PasswordSalt = saltKey;
                       request.User.Password = Encrypt.CreatePasswordHash(request.Password, saltKey);
                   }
                   break;
               default:
                   break;
           }
           request.User.Active = request.IsApproved;

           // 添加基本角色。
           //var registeredRole = _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered);
           //if (registeredRole == null)
           //    throw new PortalException("'Registered' 角色加载失败");
           if (request.User.Id == 0)
           {

               _userService.InsertUser(request.User);
               request.User = _userService.GetUserByUsername(request.Username);
           }
           //request.User.UserRoles.Add(registeredRole);
           //_userService.UpdateUser(request.User);

           return result;
       }