i am trying to generate token for userId, unfortunately i am not able to get it worked.
This is my JwtTokenGenerator class
namespace WebApiDocker.Config.Jwt
{
//https://www.c-sharpcorner.com/article/jwt-json-web-token-authentication-in-asp-net-core/
public class JwtTokenProvider
{
private readonly JwtSecurityTokenHandler _jwtTokenHandler;
private readonly AppSettings _appSettings;
public JwtTokenProvider(JwtSecurityTokenHandler jwtTokenHandler, AppSettings appSettings)
{
_jwtTokenHandler = jwtTokenHandler;
_appSettings = appSettings;
}
public string GenerateTokenForUser(int userId)
{
var secret = Encoding.ASCII.GetBytes(_appSettings.Secret);
Console.WriteLine($"Key {secret}");
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, userId.ToString())
}),
Expires = DateTime.UtcNow.AddDays(7),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha256Signature)
};
var token = _jwtTokenHandler.CreateToken(tokenDescriptor);
return _jwtTokenHandler.WriteToken(token);
}
private string GenerateTokenForNewUser()
{
var securityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_appSettings.Secret));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
var jwtToken = new JwtSecurityToken("", "", null, DateTime.Now, DateTime.Now.AddDays(7), credentials);
return _jwtTokenHandler.WriteToken(jwtToken);
}
}
}
This is how i am calling it from controller:
namespace WebApiDocker.Controllers
{
[Route("api/health")]
[ApiController]
public class HealthController : ControllerBase
{
private readonly AppSettings _appSettings;
private readonly JwtTokenProvider _jwtTokenProvider;
public HealthController(AppSettings appSettings, JwtTokenProvider jwtTokenProvider)
{
this._appSettings = appSettings;
this._jwtTokenProvider = jwtTokenProvider;
}
[HttpGet]
public IActionResult Get()
{
var statusResponse = new StatusResponse
{
Status = "Up",
AppSettings = _appSettings,
Message = _jwtTokenProvider.GenerateTokenForUser(1)
};
return Ok(statusResponse);
}
}
}
But it is not working and i am getting following exception:
ArgumentOutOfRangeException: IDX10603: Decryption failed. Keys tried: '[PII is hidden]'. Exceptions caught: '[PII is hidden]'. token: '[PII is hidden]'
Parameter name: KeySize
Microsoft.IdentityModel.Tokens.SymmetricSignatureProvider..ctor(SecurityKey key, string algorithm, bool willCreateSignatures)
Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateSignatureProvider(SecurityKey key, string algorithm, bool willCreateSignatures)
Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateForSigning(SecurityKey key, string algorithm)
Microsoft.IdentityModel.JsonWebTokens.JwtTokenUtilities.CreateEncodedSignature(string input, SigningCredentials signingCredentials)
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.CreateJwtSecurityTokenPrivate(string issuer, string audience, ClaimsIdentity subject, Nullable<DateTime> notBefore, Nullable<DateTime> expires, Nullable<DateTime> issuedAt, SigningCredentials signingCredentials, EncryptingCredentials encryptingCredentials)
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.CreateToken(SecurityTokenDescriptor tokenDescriptor)
WebApiDocker.Config.Jwt.JwtTokenProvider.GenerateTokenForUser(int userId) in JwtTokenProvider.cs
+
var token = _jwtTokenHandler.CreateToken(tokenDescriptor);
WebApiDocker.Controllers.HealthController.Get() in HealthController.cs
+
var statusResponse = new StatusResponse
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.
Looks like it is key size issue, i ran a sample test project with key as secret
which is small . I changes key to some_big_key_value_here_secret
and it worked asinformed by @Arsiwaldi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With