How to get the full WWW-Authenticate header using Dot-NET HttpClient?

Scenario

I am working on a web program for Windows Phone 8 (not so, I think it is important) and using Microsoft HTTP Client Libraries

Problem

When the user tries to get the URL, I need to know what type of authentication is needed if the answer is 401. It is very easy if it is NTLM, Basic, Digest, etc .; All supported WinHTTP schemes . You can use the following code to get the url:

HttpResponseMessage response;
using (var httpClient = new HttpClient(httpHandler))
{
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, authenticationUri);
     response = await httpClient.SendAsync(request, _cancelTokenSource.Token);
}

The easiest way to verify which authentication is needed is to use a method Headers.WwwAuthenticate.Contains, for example. to check if a circuit is needed NTLM:

string scheme = "NTLM";
bool requiredScheme = response.Headers.WwwAuthenticate.Contains(new System.Net.Http.Headers.AuthenticationHeaderValue(scheme));

If scheme = "Bearer", then it always gives false.

Getting response header

, Azure Active Directory, .

Jason Chrome

Jason Chrome App, -, :

Pragma: no-cache
Date: Fri, 10 Oct 2014 11:39:02 GMT
WWW-Authenticate: Bearer authorization_uri="https://login.windows.net/common",
    error="invalid_token", 
    error_description="The access token is missing", 
    NTLM
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Expires: -1
Cache-Control: no-cache
Content-Length: 0
X-UA-Compatible: IE=EmulateIE7

Dot-Net Http

HttpClient, System.Net.Http.HttpResponseMessage (response) :

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 0.0, Content: System.Net.Http.StreamContent, Headers:
    {
      Server: Microsoft-IIS/7.5
      WWW-Authenticate: NTLM
      X-Powered-By: ASP.NET
      X-UA-Compatible: IE=EmulateIE7
      Date: Fri, 10 Oct 2014 11:25:04 GMT
      Content-Length: 1293
      Content-Type: text/html
    }

WWW-Authenticate

.

Jason Chrome

_uri = "https://login.windows.net/common", = "invalid_token", error_description = " ",

NTLM

Dot-Net Http

NTLM

- Http (, , HttpResponseMessage), NTLM.

WWW-Authenticate Dot-NET HttpClient, ? ; () .

?


Windows.Web.Http.HttpClient

+4

All Articles