通用应用程序 NTLM 适用于 Windows 应用商店,但不适用于 Windows Phone [英] Universal App NTLM working for Windows Store, but not Windows Phone

查看:89
本文介绍了通用应用程序 NTLM 适用于 Windows 应用商店,但不适用于 Windows Phone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个通用应用程序,用于访问数据的 Web API.

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

当我在 Windows 应用商店应用程序中运行身份验证部分时,一切正常,我在登录调用时收到 200 响应(HTTP POST 调用到 _url2 在代码如下)

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)

当我从 Windows Phone 模拟器运行完全相同的代码时,我得到 401 Unauthorized.

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);
    }
}

我认为,当客户端执行 Get 以外的操作时,它似乎没有完成 NTLM 握手...我一直无法配置 Fiddler 以与我的模拟器一起工作,所以我还没有很好地了解发生了什么.通信全部通过 HTTPS,所以我也无法通过 WireShark 获得任何有用的信息.

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.

知道为什么它适用于 Windows 应用商店应用程序,而不适用于手机吗?NTLM 身份验证还有其他解决方法吗?我可以手动完成所有操作吗?

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?

推荐答案

它还远未完成,但您可以试试这个:http://uwapi.codeplex.com/

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

但是...据我所知,Windows Phone 模拟器不允许 https 连接到本地主机.也许那是你的问题.您需要添加证书以允许 https 流量.

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.

这篇关于通用应用程序 NTLM 适用于 Windows 应用商店,但不适用于 Windows Phone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆