Hi @Prathamesh Shende ,
The issue I was facing after generating JWT token where do I put that string into code to run seamlessly? every tutorial they put in postman.
From the tutorials, we can know that, after getting the JWT token, we should add it to the request header using the Authorization property.
So, it depends on which method you will using to call the API method (with the JWT token), JS or C# code?
If you want calling the API method using HttpClient, you can refer the following sample code to add the JWT token to the request header:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://localhost:44310/api/todo/");
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
var url = "relativeAddress"; //add the `[Route("relativeAddress")]` in the API action method.
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
//add jwt token to the header
var authString = "jwt token";
request.Headers.Add("Authorization", $"Bearer {authString}");
request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
Encoding.UTF8,
"application/json");//CONTENT-TYPE header
_logger.LogInformation("Create http request");
await client.SendAsync(request)
.ContinueWith(async responseTask =>
{
Console.WriteLine("Response: {0}", responseTask.Result);
var Content = await responseTask.Result.Content.ReadAsStringAsync();
});
More detailed information, see my reply in this thread.
If you want to use JQuery Ajax, the code like this:
$.ajax({
type: "POST",
url: "https://localhost:44360/Reservation",
headers: {
Authorization: 'Bearer ' + token
},
dataType: 'json',
success: function (result, status, xhr) {
ShowData(result);
},
error: function (xhr, status, error) {
alert(error);
}
});
Refer to this article: How to call a JWT secured APIs with jQuery AJAX [with source codes].
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion