Example #1
0
        public IActionResult Login([FromBody] UserInformation login)
        {
            IActionResult response = Unauthorized();
            var           user     = _JWTService.AuthenticateUserAsync(login);

            if (user.Result != null)
            {
                var tokenString = _JWTService.GenerateJSONWebToken(user.Result);
                response = Ok(new { token = tokenString });
            }

            return(response);
        }
Example #2
0
        public IActionResult Login([FromBody] UserLoginRequestModel login)
        {
            try
            {
                if (login.Username.Length < 4 || login.Username.Length > 40)
                {
                    throw new BadRequestException("Incorrect Data! Username must between " + 4 + " and " + 40 + " symbols!");
                }
                if (login.Password.Length < 6 || login.Password.Length > 40)
                {
                    throw new BadRequestException("Incorrect Data! Password must between " + 6 + " and " + 40 + " symbols!");
                }
                string jwt = _jWTService.GenerateJSONWebToken(login);

                CookieOptions cookieOptions = new CookieOptions();
                cookieOptions.Expires = DateTime.Now.AddMinutes(120);

                Response.Cookies.Append("Auth-Tst", jwt);

                IActionResult response = Ok();
                return(response);
            } catch (Exception e)
            {
                if (e.GetType().Name.Equals("BadRequestException"))
                {
                    Response.StatusCode = 400;
                    return(Content(e.Message));
                }
                else if (e.GetType().Name.Equals("NotFoundException"))
                {
                    Response.StatusCode = 404;
                    return(Content(e.Message));
                }
                else
                {
                    Response.StatusCode = 500;
                    return(Content(e.Message));
                }
            }
        }
 public string GenerateJSONWebToken(User user)
 {
     return(_jwtService.GenerateJSONWebToken(user));
 }