示例#1
0
        public ActionResult CreateAccount(string username, string password, string name)
        {
            username = username.Trim();
            if (string.IsNullOrWhiteSpace(username))
            {
                return(Json(new { status = false, message = "Failed to create user. Username cannot be empty." }));
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                return(Json(new { status = false, message = "Failed to create user. Password cannot be empty." }));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                return(Json(new { status = false, message = "Failed to create user. Name cannot be empty." }));
            }

            var account = AccountRepository.GetAccount(username);

            if (account != null && account != default(Account))
            {
                return(Json(new { status = false, message = "Failed to create user. Username already exists." }));
            }

            var salt           = PasswordHelper.CreateRandomSalt();
            var saltedPassword = PasswordHelper.ComputeSaltedHash(password, salt);

            AccountRepository.CreateAccount(username, saltedPassword, salt.ToString(), name);
            return(Json(new { status = true, message = $"User {username} created." }));
        }
示例#2
0
        public async Task CreateAsync([Required] CreateParams parameters)
        {
            // Create new Player object
            var player = new Player
            {
                Name         = parameters.Name,
                Username     = parameters.Username,
                PasswordSalt = PasswordHelper.CreateRandomSalt(),
                Email        = parameters.Email,
                Gender       = parameters.Gender,
                Birthdate    = parameters.Birthdate,
                Created      = DateTime.UtcNow
            };

            // Create random salt and hash for the password (we are not storing it in plain text)
            player.PasswordHash = PasswordHelper.ComputeHash(parameters.Password, player.PasswordSalt);

            // Check for username duplication
            var duplicate = await _repository.Players.FirstOrDefaultAsync(x => x.Username == parameters.Username);

            if (duplicate != null)
            {
                throw new SystemException("The provided username already exists.");
            }

            // Check for email duplication
            duplicate = await _repository.Players.FirstOrDefaultAsync(x => x.Email == parameters.Email);

            if (duplicate != null)
            {
                throw new SystemException("The provided email already exists.");
            }

            // Populates the Players table in the database
            _repository.Players.Add(player);

            await _repository.SaveChangesAsync();
        }