Пример #1
0
        public IHttpResponse DoRegister(DoRegisterInputModel model)
        {
            // Validate
            if (string.IsNullOrWhiteSpace(model.Username) || model.Username.Trim().Length < 4)
            {
                return(this.BadRequestError("Please provide valid username with length of 4 or more characters."));
            }

            if (string.IsNullOrWhiteSpace(model.Email) || model.Email.Trim().Length < 4)
            {
                return(this.BadRequestError("Please provide valid email with length of 4 or more characters."));
            }

            if (this.Db.Users.Any(x => x.Username == model.Username.Trim()))
            {
                return(this.BadRequestError("User with the same name already exists."));
            }

            if (string.IsNullOrWhiteSpace(model.Password) || model.Password.Length < 6)
            {
                return(this.BadRequestError("Please provide password of length 6 or more."));
            }

            if (model.Password != model.ConfirmPassword)
            {
                return(this.BadRequestError("Passwords do not match."));
            }

            // Hash password
            var hashedPassword = this.hashService.Hash(model.Password);
            var role           = Role.User;

            if (!this.Db.Users.Any())
            {
                role = Role.Admin;
            }

            // Create user
            var user = new User
            {
                Username = model.Username.Trim(),
                Email    = model.Email.Trim(),
                Password = hashedPassword,
                Role     = role
            };

            this.Db.Users.Add(user);

            try
            {
                this.Db.SaveChanges();
            }
            catch (Exception e)
            {
                // TODO: Log error
                return(this.ServerError(e.Message));
            }

            // Redirect
            return(this.Redirect("/Users/Login"));
        }
Пример #2
0
        public IHttpResponse Register(DoRegisterInputModel model)
        {
            if (User.IsLoggedIn)
            {
                return(Redirect("/"));
            }

            if (string.IsNullOrWhiteSpace(model.Username) || model.Username.Trim().Length < 4)
            {
                return(BadRequestErrorWithView("Please provide valid username with length of 4 or more characters.", model));
            }

            if (string.IsNullOrWhiteSpace(model.Email) || model.Email.Trim().Length < 4)
            {
                return(BadRequestErrorWithView("Please provide valid email with length of 4 or more characters.", model));
            }

            if (Db.Users.Any(x => x.Username == model.Username.Trim()))
            {
                return(BadRequestErrorWithView("User with the same name already exists.", model));
            }

            if (string.IsNullOrWhiteSpace(model.Password) || model.Password.Length < 6)
            {
                return(BadRequestErrorWithView("Please provide password of length 6 or more.", model));
            }

            if (model.Password != model.ConfirmPassword)
            {
                return(BadRequestErrorWithView("Passwords do not match.", model));
            }

            var hashedPassword = _hashService.Hash(model.Password);

            var role = Role.User;

            if (!Db.Users.Any())
            {
                role = Role.Admin;
            }

            var user = new User
            {
                Username = model.Username.Trim(),
                Email    = model.Email.Trim(),
                Password = hashedPassword,
                Role     = role
            };

            Db.Users.Add(user);

            try
            {
                Db.SaveChanges();
            }
            catch (Exception e)
            {
                return(BadRequestErrorWithView(e.Message));
            }

            return(Redirect("/users/login"));
        }