在Xamarin.iOS中使用NTLM身份验证获取401

时间:2017-12-13 14:14:23

标签: c# xamarin.ios httpclient ntlm

我已经尝试过每一个我能找到的博客。但我无法通过NTLM挑战解决状态401。

所以基本上我需要获得一个托管在公司服务器上的用户的个人资料图片。

当我点击浏览器中的网址时它会提示输入用户名和密码,一旦我提供了用户名和密码,我就会得到图片。

但同样我无法在Xamarin.iOS中实现

以下是我正在尝试的代码段。

            var credentials = new NetworkCredential("myUserName", "Password", "domain");

        var handler = new HttpClientHandler { Credentials = credentials, UseDefaultCredentials = false };
        handler.AllowAutoRedirect = false;
        var client = new HttpClient(handler);
        try
        {
            var response = await client.GetAsync("https://mycustomUrl.net/User%20Photos/Profile%20Pictures/eurn_martin_kremmer_MThumb.jpg");
            Console.WriteLine(response);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

我得到的回应是

    StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Microsoft-IIS/8.0
SPRequestGuid: 5725369e-b047-40c5-d468-0417992ffab3
request-id: 5725369e-b047-40c5-d468-0417992ffab3
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 2
SPIisLatency: 0
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4779
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Wed, 13 Dec 2017 13:55:26 GMT
Content-Length: 0
}

1 个答案:

答案 0 :(得分:1)

哇,当你用NSURLConnection抛出相同的请求时,我不知道解决方案就像馅饼一样简单。

这里,

win = new BrowserWindow({
        width: 900,
        height: 680,
        webPreferences: {
            webSecurity: false
        }
});
//second alternative
app.commandLine.appendSwitch('ignore-certificate-errors');

这在Citrix MAM环境中无需麻烦。

但是要在模拟器中测试它,你需要处理如下的挑战。

responseData = new NSMutableData();
request = new NSMutableUrlRequest(uri,NSUrlRequestCachePolicy.UseProtocolCachePolicy, 6000);
request.HttpMethod = "GET";
 connection = new NSUrlConnection(request, this);