Esempio n. 1
0
        public IActionResult RegisterAccount(RegisterUserInput inputUser)
        {
            if (string.IsNullOrWhiteSpace(inputUser.Username) || string.IsNullOrWhiteSpace(inputUser.Password) ||
                string.IsNullOrWhiteSpace(inputUser.Captcha))
            {
                _logger.LogInformation("Username, password or captcha is empty.");
                return(BadRequest());
            }

            if (inputUser.Username.Length > 50)
            {
                _logger.LogInformation("Username exceeds permitted length.");
                return(BadRequest());
            }

            if (!_captcha.VerifyCaptcha(inputUser.Captcha, HttpContext.Connection.RemoteIpAddress, "register"))
            {
                _logger.LogInformation("Captcha failed verification");
                return(BadRequest());
            }

            if (Guid.TryParse(inputUser.Token, out _) &&
                _memoryCache.TryGetValue("R1" + inputUser.Token, out User cachedUser))
            {
                _memoryCache.Remove("R1" + inputUser.Token);
                cachedUser.Password = inputUser.Password;
                cachedUser.Username = inputUser.Username.ToLower();
                if (_authHandler.RegisterUser(ref cachedUser))
                {
                    _activityLogger.LogRegister(Request.HttpContext.Connection.RemoteIpAddress, cachedUser);
                    return(Ok());
                }

                _logger.LogInformation("Auth handler rejected account.");
                return(BadRequest());
            }

            _logger.LogInformation("Token is invalid.");
            return(BadRequest());
        }