TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jes Sie
741
1.2k
281.8k
JWT is not well formed
Jun 3 2021 8:15 AM
Again my first time developing an asp.net core rest API. See below snippet:
And below is the error:
"errorMessage": "System.ArgumentException: IDX12709: CanReadToken() returned false. JWT is not well formed: 'System.String'.\nThe token needs to be in JWS or JWE Compact Serialization Format. (JWS): 'EncodedHeader.EndcodedPayload.EncodedSignature'. (JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'.\r\n at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ReadJwtToken(String token)\r\n at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters)\r\n at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.
Below also is my entire TokenManager:
public
class
TokenManager : ITokenManager
{
//private List<Token> listTokens;
private
JwtSecurityTokenHandler tokenHandler;
private
byte
[] secretKey;
public
TokenManager()
{
tokenHandler =
new
JwtSecurityTokenHandler();
secretKey = Encoding.ASCII.GetBytes(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
//listTokens = new List<Token>();
}
public
bool
Authenticate(
string
userName,
string
password)
{
if
(!
string
.IsNullOrEmpty(userName) &&
!
string
.IsNullOrEmpty(password) &&
userName.ToLower() ==
"ltcadmin"
&&
password ==
"ltcpassword"
)
return
true
;
else
return
false
;
}
public
string
NewToken()
{
var tokenDescriptor =
new
SecurityTokenDescriptor
{
Subject =
new
ClaimsIdentity(
new
Claim[] {
new
Claim(ClaimTypes.Name,
"LTC LMM"
) }),
Expires = DateTime.UtcNow.AddMinutes(5),
SigningCredentials =
new
SigningCredentials(
new
SymmetricSecurityKey(secretKey),
SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
var jwtString = tokenHandler.WriteToken(token);
return
jwtString;
}
public
ClaimsPrincipal VerifyToken(
string
token)
{
var claims = tokenHandler.ValidateToken(token,
new
TokenValidationParameters
{
ValidateIssuerSigningKey =
true
,
IssuerSigningKey =
new
SymmetricSecurityKey(secretKey),
ValidateLifetime =
true
,
ValidateAudience =
false
,
ValidateIssuer =
false
,
ClockSkew = TimeSpan.Zero
},
out
SecurityToken validatedToken);
return
claims;
}
It says that the error is in line 53. Thank you in advance.
Reply
Answers (
1
)
How to create a Web service to call a Stored Procedure
Using HttpClient over an SSH tunnel