Пример #1
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);
        }
Пример #3
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;
       }