public IActionResult Login([FromBody] AccountDto user)
        {
            var account = _userLogic.Authenticate(user.UserCode, user.Password);

            if (user == null)
            {
                return(BadRequest("Invalid client request"));
            }

            if (account == null)
            {
                return(Unauthorized());
            }

            var secretKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345"));
            var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256);
            var claims            = new[] {
                new Claim("Identifier", user.UserCode),
            };

            var tokeOptions = new JwtSecurityToken(
                issuer: "http://localhost:44304",
                audience: "http://localhost:44304",
                claims: claims,
                expires: DateTime.Now.AddMinutes(5),
                signingCredentials: signinCredentials
                );

            var tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions);

            return(Ok(new { Token = tokenString, Role = account.Role }));
        }
Exemplo n.º 2
0
        public IActionResult Authenticate([FromBody] UserAuthenticationDto userDto)
        {
            var user = _userLogic.Authenticate(userDto.Username, userDto.Password);

            if (user == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            return(Ok(new
            {
                user.Id,
                user.Username,
                user.FirstName,
                user.LastName,
                user.Email,
                Token = tokenString
            }));
        }
Exemplo n.º 3
0
        public IActionResult Authenticate(User user)
        {
            var entity = _logic.Authenticate(user.Email, user.Password);

            if (entity == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, entity.Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            // return basic user info (without password) and token to store client side
            return(Ok(new
            {
                Id = entity.Id,
                Username = entity.Email,
                Name = entity.Name,
                Token = tokenString
            }));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Authenticate([FromBody] AuthenticateRequest model)
        {
            var response = await _userLogic.Authenticate(model);

            return(response.HasNoValue()
                ? Unauthorized(new { message = "Username or password is incorrect" })
                : Ok(response));
        }
        public IActionResult Authenticate([FromBody] UserDto userParam)
        {
            var user = _userLogic.Authenticate(userParam.Username, userParam.Password);

            if (user == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            return(Ok(user));
        }
Exemplo n.º 6
0
        public async Task <ActionResult <UserAuthenticatedDto> > Authenticate([FromBody] AuthenticateDto model)
        {
            //var claimsIdentity = this.User.Identity as ClaimsIdentity;
            //var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
            var authenticateUserModel = _mapper.Map <AuthenticateUserModel>(model);
            var user = await _userLogic.Authenticate(authenticateUserModel);

            if (user == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            return(Ok(user));
        }
Exemplo n.º 7
0
        public IActionResult Authenticate([FromBody] LoginReq loginReq)
        {
            try
            {
                var user = _userLogic.Authenticate(loginReq);

                if (user == null)
                {
                    return(BadRequest(new { message = "Username or password is incorrect" }));
                }

                return(Ok(user));
            }
            catch (Exception ex)
            {
                return(Ok(this.BaseResponseApiErrorResult(ex)));
            }
        }