private string GenerateJwt(ClaimsIdentity identity, string userName, JsonSerializerSettings serializerSettings) { var response = new { id = identity.Claims.Single(c => c.Type == "id").Value, auth_token = _jwtGenerator.GenerateEncodedToken(userName, identity), expires_in = (int)_jwtOptions.ValidFor.TotalSeconds }; return(JsonConvert.SerializeObject(response, serializerSettings)); }
public static async Task <string> GenerateJwt(ClaimsIdentity identity, IJwtGenerator jwtGenerator, string emailAddress, JwtIssuerOptions jwtOptions, JsonSerializerSettings serializerSettings) { var response = new { id = identity.Claims.Single(c => c.Type == "id").Value, role = identity.Claims.Single(c => c.Type == "rol").Value, auth_token = await jwtGenerator.GenerateEncodedToken(emailAddress, identity), expires_in = (int)jwtOptions.ValidFor.TotalSeconds }; return(JsonConvert.SerializeObject(response, serializerSettings)); }
public ActionResult Token(LoginModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var username = model.Email; var password = model.Password; var user = repository.FindOne(x => x.Email == username && x.Password == password); var identity = jwtGenerator.GenerateClaimsIdentity(user); if (identity == null) { return(BadRequest("Invalid username or password.")); } var encodedJwt = jwtGenerator.GenerateEncodedToken(identity); var role = identity.Claims.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).First(); var response = new ResponceLogin(encodedJwt, identity.Name, role, user.Avatar); return(Ok(response)); }