0

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?

joe_coolish
  • 7,201
  • 13
  • 64
  • 111
  • So, you have tested it in the emulator, but have you tested the code on a windows phone device? – Jordan Aug 25 '14 at 12:10
  • I have not. I actually don't have a dev-unlocked device :/ – joe_coolish Aug 25 '14 at 12:38
  • Is the server the app is connecting to running IIS? http://support.microsoft.com/kb/2749007 So- according to that link the Pre-authentication is what might cause that error. You could try setting PreAuthenticate to false and see if that helps too. – Jordan Aug 25 '14 at 13:19
  • It is running on IIS. I'll check that out when I get home and see if that changes anything :) – joe_coolish Aug 25 '14 at 13:30
  • It might also be connected to this issue: http://stackoverflow.com/questions/24922929/401-unauthorized-on-second-httpclient-httpwebrequest-call – Daniel Sklenitzka Oct 10 '14 at 12:41
  • I did find a solution to this... I started using the `HttpWebRequest` version and it worked! No idea why... – joe_coolish Oct 15 '14 at 19:33

1 Answers1

0

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.