0

I want to use JWT authorization for .net core MVC project. I've found a number of examples of how to return a JWT token when a user logs in. However, there are only a few examples of how to transmit a token at each request from the header.

I found example with AJAX:

$.ajaxSetup({ headers: { "Authorization": "Bearer " + accessToken } });

Simple Authentication using Jwt in dot net core MVC

How to use JWT in MVC application for authentication and authorization?

Is this the right way to transfer a JWT token? Do I always have to use AJAX?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
TomTom
  • 91
  • 1
  • 11

2 Answers2

0

The code samples in your links are using Ajax to access the protected resources/apis . You can also pass the bearer token in headers to make api calls on server side :

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "APIurl");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "YourAccessToken");
HttpResponseMessage response = await client.SendAsync(request);


if (response.IsSuccessStatusCode)
{

    String responseString = await response.Content.ReadAsStringAsync();

    ...
}
Nan Yu
  • 26,101
  • 9
  • 68
  • 148
-1

There is no such thing as JWT authorisation.

JWT tokens are just a format for distributing signed claim-sets.

The correct way of passing tokens around depends on many factors, such as where you are in an oauth flow, if the transport is encrypted, if the tokens contain personal info, and if your front end is spa, native etc

Sentinel
  • 3,582
  • 1
  • 30
  • 44
  • I downloaded a project: https://www.codeproject.com/Articles/1203978/JWT-Security-Part-Secure-MVC-application – TomTom Mar 21 '19 at 19:48