Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

HttpClient not accepting Authorization headers (401 Unauthorized) #3918

Closed
dush88c opened this issue Sep 27, 2018 · 6 comments
Closed

HttpClient not accepting Authorization headers (401 Unauthorized) #3918

dush88c opened this issue Sep 27, 2018 · 6 comments

Comments

@dush88c
Copy link

dush88c commented Sep 27, 2018

I have used System.Net.Http to create an HTTP client to access the Web API GET method which is authorized as below. But it returns unauthorized (401) error always even if it works fine in Postman with same authorization token (Bearer)

using (var ProfileClient = new HttpClient())
            {
                var token = OAuthToken.Instance.AccessToken;
                ProfileClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                
                var response = await ProfileClient.GetAsync(AppSettings.AbbottAuthorizationProfileUrl);
                
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return CheckProfileReturn(response);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    return new Tuple<bool, string>(false, "Unauthorized access");
                }
                else
                {
                    var result = await response.Content.ReadAsStringAsync();
                    var errorModel = JsonConvert.DeserializeObject<OauthError>(result);
                    return new Tuple<bool, string>(false, errorModel?.ErrorDescription);
                }
            }

I have no idea why this is not working in Xamarin.Forms httpclient ?
Versions
Xamarin Forms : 2.4.0.280
System.Net.Http : MonoAndroid\v1.0\4.0.30319
http
.Net : 4.5

Thanks

@rmarinho
Copy link
Member

Hi this doesn't seem a issue related with Xamarin.Forms itself.

Trying to help you might want to migrate to use the latest version of Xamarin Forms like 3.2.0 that uses netstandard .

You should inspect the headers of the call to see how the request is form. also might want to check if its a https issue or something.

Feel free to reopen if you figure is something wrong with Xamarin.Forms.

Thanks

@Adincodavid
Copy link

@dush88c dis you solved this issue??
I have the same problem

@dush88c
Copy link
Author

dush88c commented Nov 15, 2019

In my case, there was '/' at end of the url. Once it's removed. It worked.

@amirvenus
Copy link

I have the same issue
I can obtain the token using MSAL and in fact, I can use that bearer token in Postman but when I pass it to my HttpClient, I keep getting 401.

I think this is rather a mono bug...

@poorankharol
Copy link

I have the same issue does not work on android app but works on postman

@Mirazyzz
Copy link

@amirvenus @rmarinho did you found out the reason of this issue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants