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

问题描述 投票:1回答:1

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

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

当我在浏览器中点击URL时它提示输入用户名和密码,一旦我提供它,我就会得到图像。

但同样我无法在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
}
c# xamarin.ios httpclient ntlm
1个回答
1
投票

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

这里,

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

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

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

        public override void WillSendRequestForAuthenticationChallenge(NSUrlConnection connection, NSUrlAuthenticationChallenge challenge)
    {
        Console.WriteLine(challenge.ProtectionSpace);

        if (challenge.PreviousFailureCount == 0 )
        {

            NSUrlCredential cred = new NSUrlCredential(this.myUserName, this.myPass, NSUrlCredentialPersistence.None);
            challenge.Sender.UseCredential(cred, challenge);
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.