Beispiel #1
0
        public async Task <IActionResult> Authorization([FromBody] UserModel userv)
        {
            IPHostEntry heserver = Dns.GetHostEntry(Dns.GetHostName());
            var         ip       = heserver.AddressList[2].ToString();
            // var ip = "0.0.0.0";
            var       userAgent = HttpContext.Request.Headers["User-Agent"].ToString();
            UserModel user;

            if (string.IsNullOrEmpty(userv.Login) || string.IsNullOrEmpty(userv.Password))
            {
                _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n One of fields is empty");
                return(Conflict("Fields required"));
            }

            if (Regex.IsMatch(userv.Login, @"[^\w\.@-\\%]"))
            {
                if (_userModelRepository.GetByEmail(userv.Login) == null)
                {
                    _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                       "\n User-agent : " + userAgent +
                                       "\n EMail: " + userv.Login +
                                       "\n User not found by email");
                    return(Content("User not found by email" !));
                }
                else
                {
                    user = _userModelRepository.GetByEmail(userv.Login);
                }
            }
            else
            {
                if (_userModelRepository.GetByLogin(userv.Login) == null)
                {
                    _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                       "\n User-agent : " + userAgent +
                                       "\n Login: "******"\n User not found by login");
                    return(Content("User not found" !));
                }
                else
                {
                    user = _userModelRepository.GetByLogin(userv.Login);
                }
            }

            SHA1 sha1Hash = SHA1.Create();

            byte[] sourceBytes = Encoding.UTF8.GetBytes(userv.Password);
            byte[] hashBytes   = sha1Hash.ComputeHash(sourceBytes);
            string hashPass    = BitConverter.ToString(hashBytes).Replace("-", String.Empty);

            if (!user.Password.Equals(hashPass))
            {
                _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n Password mismatch!");
                return(Content("Password mismatch!"));
            }

            _logger.LogInformation("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n Authorization was successful!");
            await Authenticate(user.Login);

            return(Ok(user));
        }
Beispiel #2
0
        public IActionResult UserRegistration(UserModel user)
        {
            if (string.IsNullOrEmpty(user.Login))
            {
                return(BadRequest("Login can't be empty"));
            }
            else if (string.IsNullOrEmpty(user.Password))
            {
                return(BadRequest("Password can't be empty"));
            }
            else if (string.IsNullOrEmpty(user.Mail))
            {
                return(BadRequest("Email can't be empty"));
            }

            IPHostEntry heserver  = Dns.GetHostEntry(Dns.GetHostName());
            var         ip        = heserver.AddressList[2].ToString();
            var         userAgent = HttpContext.Request.Headers["User-Agent"].ToString();


            if (!string.IsNullOrEmpty(_userModelRepository.GetByLogin(user.Login).Login))
            {
                _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n Non unique LOGIN!");
                return(BadRequest("This login is busy, use another"));
            }

            if (!string.IsNullOrEmpty(_userModelRepository.GetByEmail(user.Mail).Mail))
            {
                _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n EMail: " + user.Mail +
                                   "\n Non unique Email address!");
                return(BadRequest("Email should be unique"));
            }


            if (user.DOB > DateTime.Now)
            {
                _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n Can't be future date ");
                return(BadRequest("Date of birth problem"));
            }


            if (Regex.IsMatch(user.Login, @"[^\w\.@-\\%]"))
            {
                _logger.LogWarning("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n Login can't contains server symbol");
                return(BadRequest("Login can't contains service symbol"));
            }

            SHA1 sha1Hash = SHA1.Create();

            byte[] sourceBytes = Encoding.UTF8.GetBytes(user.Password);
            byte[] hashBytes   = sha1Hash.ComputeHash(sourceBytes);
            string hashPass    = BitConverter.ToString(hashBytes).Replace("-", String.Empty);

            user.Password = hashPass;
            _userModelRepository.AddNew(user);

            _logger.LogInformation("[AuthorizationController.Authorization] " + "\n Remote ip : " + ip +
                                   "\n User-agent : " + userAgent +
                                   "\n Login: "******"\n Email: " + user.Mail +
                                   "\n Date of Birth: " + user.DOB +
                                   "\n New User was adding!");
            return(Ok());
        }