'JWT configuration Encoding.UTF8
I would like to implement JWT-based authentication to our new REST API. But
var authSigningKey in every response return null, in fact the problem is
Encoding.UTF8.GetBytes(_configuration["JWT:Key"])
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Key"]));
this is my Jwt Appsetting:
"JWT": {
"Key": "DhftOS5uphK3vmCJQrexST1RsyjZBjXWRgJMFPU4",
"ValidAudience": "http://localhost:44356",
"ValidIssuer": "http://localhost:44356"
}
and it is my code:
private JwtSecurityToken GetToken(List<Claim> authClaims)
{
try
{
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Key"]));
var token = new JwtSecurityToken(
issuer: _configuration["JWT:ValidIssuer"],
audience: _configuration["JWT:ValidAudience"],
expires: DateTime.Now.AddHours(3),
claims: authClaims,
signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
);
return token;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
in every encoding it is null.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
