C
C#12mo ago
baristaner

How can i use ASP.NET CORE Identity with JWT

Hello guys i'm using Identity Package for authentication,and i want to implement JWT TOKEN to my auth system Right now endpoints that comes with Identity works fine it returns token but it doesnt look like a JWT Token The Login EP Returns
{
CfDJ8ALPmkxk6X1NoFcQPGKzyblX6sAbwACCXF3V2byfWzg7QhfSa47_VEp2AIhtUk9sY9ylJuQZowEkHYBydFCW-cbAMgnRmcv2BdH8bRTRNsgLkBskszwj-G4OOqz31CrfkQLKNDAVW497BnL240v8IGxTxUxw5tsn0X7bbuJmcq7jC4K6G36eBBSVk7WtaGz3C3ZqgkTCgoyTAX1QrL8dl7t3yW6tZqczdwPTWyC7KYWQPqqGXwZAFy8VyL1gsv_P2_kRN8G9OowsyKuB4Z4hJ3M6ZWQBxMPy2AH-j91i6CCuKIjcD6GH3QR82ZDN-Ly-H-cJwB721dUOR0lrNVXijaoGEYPPj0YcbGHxn3sUwAfZlT9n3-qVhGs65HlA8KtkQa-uo6deKsP4ENJOopnfT8g76JovsDSx0QNXhlgQBNX9aoNDrCU6BJmJlFhm5THMxbTyeKT6uQXQgN811B4SivmeIIwNZS9sf9S8hBYAHFDiExECIZo6g8PBjWUzoJ2eiEozuazFku57twICxIUZT3JMWolozK3Ms1v76tzU-xa120geZ9A5qXyBu_fr_RcycMan1JN0w2-1xcR2w4b7x_BagSagc9N3QyutXWugBogBiHQC5j9nOFgF983Fgd426SgSk_aW_59538u5uhmWmpiwsIki9H0O5AmsIR_glQvnEoeaEY216RQuuDGcm-WkqA,
}
{
CfDJ8ALPmkxk6X1NoFcQPGKzyblX6sAbwACCXF3V2byfWzg7QhfSa47_VEp2AIhtUk9sY9ylJuQZowEkHYBydFCW-cbAMgnRmcv2BdH8bRTRNsgLkBskszwj-G4OOqz31CrfkQLKNDAVW497BnL240v8IGxTxUxw5tsn0X7bbuJmcq7jC4K6G36eBBSVk7WtaGz3C3ZqgkTCgoyTAX1QrL8dl7t3yW6tZqczdwPTWyC7KYWQPqqGXwZAFy8VyL1gsv_P2_kRN8G9OowsyKuB4Z4hJ3M6ZWQBxMPy2AH-j91i6CCuKIjcD6GH3QR82ZDN-Ly-H-cJwB721dUOR0lrNVXijaoGEYPPj0YcbGHxn3sUwAfZlT9n3-qVhGs65HlA8KtkQa-uo6deKsP4ENJOopnfT8g76JovsDSx0QNXhlgQBNX9aoNDrCU6BJmJlFhm5THMxbTyeKT6uQXQgN811B4SivmeIIwNZS9sf9S8hBYAHFDiExECIZo6g8PBjWUzoJ2eiEozuazFku57twICxIUZT3JMWolozK3Ms1v76tzU-xa120geZ9A5qXyBu_fr_RcycMan1JN0w2-1xcR2w4b7x_BagSagc9N3QyutXWugBogBiHQC5j9nOFgF983Fgd426SgSk_aW_59538u5uhmWmpiwsIki9H0O5AmsIR_glQvnEoeaEY216RQuuDGcm-WkqA,
}
And here is my AddAuthentication in Program cs
c#
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("JWT_SECRET")),

ValidateIssuer = true,
ValidIssuer = "https://localhost:5000/", // Your identity provider URL

ValidateAudience = true,
ValidAudience = "https://localhost:5001/", // Your resource/API URL

ValidateLifetime = true
};
});
c#
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("JWT_SECRET")),

ValidateIssuer = true,
ValidIssuer = "https://localhost:5000/", // Your identity provider URL

ValidateAudience = true,
ValidAudience = "https://localhost:5001/", // Your resource/API URL

ValidateLifetime = true
};
});
4 Replies
baristaner
baristanerOP12mo ago
The token doesn't seem right to me and i can't decode it
Jimmacle
Jimmacle12mo ago
i don't believe there's a default implementation to make identity issue JWTs, all the examples i've seen online show implementing a custom controller
baristaner
baristanerOP12mo ago
Okay thanks
blueberriesiftheywerecats
You have to make service that will generate jwt for you based on needed claims

Did you find this page helpful?