1

I use NSwagStadio to write httpClient codes.I use authorization in api. I can login and register with httpClient codes but when I want to access apiControllers that need authentication I get 401 status code.All codes work correct if I remove authorization from apiController. I think I found where is the problem but I dont know how to fix it.

here is the problem in codes that NSwag made:

 var urlBuilder_ = new System.Text.StringBuilder();
            urlBuilder_.Append("api/LeaveTypes");

            var client_ = _httpClient;
            var disposeClient_ = false;
            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    request_.Method = new System.Net.Http.HttpMethod("GET");
                    request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));

                    PrepareRequest(client_, request_, urlBuilder_);

                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
                    var disposeResponse_ = true;

in pictures below you can see client_ have authentication header but request_ authentication header is null so response_ get 401 error. here is the picture that shows client have authorization header: enter image description here

and here is the picture that shows request doesnt have authorization header: enter image description here

I added manually this code but still doesnt work


                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

                    PrepareRequest(client_, request_, url_);

                    //I add this line
                    request_.Headers.Authorization = new AuthenticationHeaderValue(client_.DefaultRequestHeaders.Authorization.Scheme, client_.DefaultRequestHeaders.Authorization.Parameter);
                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
                    var disposeResponse_ = true;

the token is correct.I used the token in api project and it works but httpClient doesnt send the token

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
Ali M
  • 11
  • 1
  • Could you try this `request_.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "paste_access_token_here");` or `client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "paste_access_token_here");` – Jason Pan Feb 21 '23 at 06:02

2 Answers2

0

In your code, client_.DefaultRequestHeaders.Authorization.Scheme seems null here, so that you get the issue. You can try to code below. Or add Authorization.Scheme when you create the var client_ = _httpClient;.

I have tried below code.

request_.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "paste_access_token_here");

Here is my test result:

enter image description here

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • hi. request_ have values when I add this code: request_.Headers.Authorization = new AuthenticationHeaderValue(client_.DefaultRequestHeaders.Authorization.Scheme, client_.DefaultRequestHeaders.Authorization.Parameter); . but after line response_ generated again error 401 returned and in line disposeResponse_ breakpoint again request_ authorization header is null – Ali M Feb 22 '23 at 04:43
  • @AliM I mean this is null, or you can share us you sample code and tell us how to reproduce the issue, pls hide your sensitive info. – Jason Pan Feb 22 '23 at 10:06
  • finally I found the problem. it was the url. the url that I used was http not https – Ali M Feb 22 '23 at 10:30
0

finally I found the problem. it was the url. the url that I used was http not https so request lost its header the problem was in httpClient.BaseAddress in program.cs:

 builder.Services.AddHttpClient<IClient, Client>(cl => cl.BaseAddress = new Uri("https://localhost:7132"));

api have two url one https and one http. I used http instead of https. it works fine before I add authentication to the api project but after adding it http doesnt work so after changing http url to https it works well

Ali M
  • 11
  • 1