ta dando HTTP ERROR 401 ja fiz a conferencia do código e só da esse erro na pagina e no postman na compilacao nao esta apresentando nenhum erro
LOGINCONTROLLER
namespace Alura.WebAPI.WebApp.Api { [ApiController] [Route("api/[controller]")] public class LoginController : ControllerBase { private readonly SignInManager _signInManager;
public LoginController(SignInManager<Usuario> signInManager)
{
_signInManager = signInManager;
}
[HttpPost]
public async Task<IActionResult> Token(LoginModel model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Login, model.Password, true, true);
if (result.Succeeded)
{
//cria token (header + payload + signature)
var direitos = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, model.Login),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var chave = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes("alura-webapi-authentication-valid"));
var credenciais = new SigningCredentials(chave, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: "Alura.WebApp",
audience: "Postman",
claims: direitos,
signingCredentials: credenciais,
expires: DateTime.Now.AddMinutes(30)
);
var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
return Ok(tokenString);
}
return Unauthorized(); //401
}
return BadRequest(); //400
}
}
}
STARTUP
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "JwtBearer";
options.DefaultChallengeScheme = "JwtBearer";
}).AddJwtBearer("JwtBearer", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes("alura-webapi-authentication-valid")),
ClockSkew = TimeSpan.FromMinutes(5),
ValidIssuer = "Alura.WebApp",
ValidAudience = "Postman",
};
});