public async Task <IActionResult> Post([FromBody] CredentialsViewModel credentials) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var identity = await GetClaimsIdentity(credentials.UserName, credentials.Password); if (identity == null) { return(BadRequest(Error.AddErrorToModelState("login_failure", "Invalid username or password.", ModelState))); } // Serialize and return the response var response = new { id = identity.Claims.Single(c => c.Type == "id").Value, auth_token = await _jwtFactory.GenerateEncodedToken(credentials.UserName, identity), expires_in = (int)_jwtOptions.ValidFor.TotalSeconds }; var json = JsonConvert.SerializeObject(response, _serializerSettings); return(new OkObjectResult(json)); }
public async Task <IActionResult> Post([FromBody] UserDto userDto) { try { User LoggedInUser = _login.Login(userDto.Username, userDto.Password); if (LoggedInUser.Id != 0) { var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("ABCneedtogetthisfromenvironmentXYZ")); var signinCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256); var tokeOptions = new JwtSecurityToken( issuer: "http://localhost:50596", audience: "http://localhost:50596", claims: new List <Claim>(), expires: DateTime.Now.AddMinutes(5), signingCredentials: signinCredentials ); var tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions); return(Ok(new { Token = tokenString, User = LoggedInUser })); } else { return(Unauthorized()); } } catch (System.Exception ex) { //Console.WriteLine(ex.Message); return(BadRequest(Error.AddErrorToModelState("login_failure", "Invalid username or password.", ModelState))); } }