コード例 #1
0
        public Boolean Register(User user)
        {
            user.Username = user.Username.Trim();
            user.LoweredUsername = user.Username.ToLower();
            user.Password = user.Password.Trim();
            user.EmailAddress = user.EmailAddress.Trim();

            if (!IsUserValid(user, new List<String>()))
                return false;

            try
            {
                using (var session = _sessionManager.GetSession())
                {
                    using (var tx = session.BeginTransaction())
                    {

                        session.SaveOrUpdate(user);
                        tx.Commit();
                    }
                }
            }
            catch
            {
                return true;
            }

            return true;
        }
コード例 #2
0
        public RegistrationValidationResultDto IsRegistrationValid(String username, String password, String email)
        {
            var result = new RegistrationValidationResultDto();
            result.UserId = Guid.NewGuid().ToString();
            var errors = new List<String>();

            username = username.Trim();
            password = password.Trim();
            email = email.Trim();

            User user = new User() { Username = username.Trim(), EmailAddress = email.Trim(), Password = password.Trim(), Id = Guid.NewGuid().ToString(), LoweredUsername = username.Trim().ToLower() };

            result.IsValid = IsUserValid(user, errors);
            result.Errors = errors.ToArray();
            return result;
        }
コード例 #3
0
        private Boolean IsUserValid(User user, List<String> errors)
        {
            if (user.Username.Length <= 4 || user.Username.Length > 20)
            {
                errors.Add("Username must be 5 to 20 characters long.");
            }

            if (user.Password.Length <= 4 || user.Password.Length > 20)
            {
                errors.Add("Password must be 5 to 20 characters long.");
            }

            if (!Regex.IsMatch(user.EmailAddress, @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"))
            {
                errors.Add("Please enter a valid email address.");
            }

            using (var session = _sessionManager.GetSession())
            {
                if (!String.IsNullOrEmpty(user.Username))
                {
                    var results = session.Linq<User>()
                        .Where(u => u.LoweredUsername == user.LoweredUsername);

                    if (results.ToList().Count() > 0)
                        errors.Add("Username is already in use.");
                }
            }

            if (errors.Count > 0)
                return false;

            return true;
        }