简体   繁体   中英

Universal App NTLM working for Windows Store, but not Windows Phone

I am building a Universal App that accesses a web API for data.

When I run the authentication piece in the Windows Store app, everything works and I get a 200 response on my login call (an HTTP POST call to _url2 in the code below)

When I run the exact same code from the Windows Phone emulator, I get a 401 Unauthorized.

Here is the code I'm using to access the service:

var handler = new HttpClientHandler
{
    AllowAutoRedirect = true,
    PreAuthenticate = true,
    CookieContainer = _cookies,
    Credentials = new NetworkCredential(username, password),
    UseCookies = true,
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
using (var client = new HttpClient(handler, true))
{
    //client.DefaultRequestHeaders.Connection.Add("keep-alive");
    //  Having the Connection = keep-alive causes the phone to throw an exception... not needed, but annoying

   client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));

    var res = await client.GetAsync(_url1);  // This works and will negotiate NTLM on both platforms.  Returns 200 on both Phone and Store apps 

    client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");
    client.DefaultRequestHeaders.Referrer = new Uri(_url1);
    client.DefaultRequestHeaders.Add("Origin", _url1);

    using (var message = new HttpRequestMessage(HttpMethod.Head, _url2))
    {
        var header = await client.SendAsync(message);  // 401 on Phone, 200 on Store app
    }

    var resp = await client.PostAsync(_url2, new StringContent(LoginContent));   // 401 on Phone, 200 on Store app

    using (var stream = await resp.Content.ReadAsStreamAsync())
    using (var reader = new StreamReader(stream))
    {
        var html = await reader.ReadToEndAsync();

        ParseLoginResults(html);
    }
}

I think that when the client does something other than Get , it seems to not complete the NTLM handshake... I haven't been able to configure Fiddler to work with my emulator, so I haven't gotten a good trace on what is going on. The communication is all over HTTPS so I can't get anything useful over WireShark either.

Any idea why it works on Windows Store apps, but not on the phone? Are there any other work arounds for NTLM authentication? Can I just do everything manually?

it´s far from completed but you could try this : http://uwapi.codeplex.com/

However...as far as i know windows phone emulator does not allow https connections to localhost. maybe that´s your problem. You need to add a certificate to allow https traffic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM