简体   繁体   中英

NTLM authentication not working when using Windows.Web.HttpClient having AllowUI set false

I am porting my windows 8.1 app to windows 10 uwp app. The problem I have encountered is that old code (it is still compiling though) that uses System.Net.HttpClient throws some stupid exception when I am trying to authenticate on the server using NTLM.

"Known Windows 10 SDK error - we are fixing it." - all help from Microsoft since months. (described by someone else here: https://social.msdn.microsoft.com/Forums/en-US/9e137127-e0e5-4aec-a7a9-d66f5b84c70b/rtm-known-issue-systemnethttphttpclient-or-httpwebrequest-class-usage-in-a-uwp-app-throws-a?forum=Win10SDKToolsIssues )

The only workaround? - "Use Windows.Web.HttpClient instead"

So I am trying.

var request = new HttpRequestMessage();

request.RequestUri = MyURI;
request.Method = HttpMethod.Post;
request.Content = new HttpStringContent(MyContent);

request.Headers.Add("User-Agent", "MyApp");
request.Headers.Add("SOAPAction", "MySoapAction");
request.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("text/xml; charset=utf-8");

var filter = new HttpBaseProtocolFilter();
filter.AllowAutoRedirect = true;
filter.AllowUI = true;  
filter.ServerCredential = new PasswordCredential(address, username, password);

HttpClient client = new HttpClient(filter);        

var response = await client.SendRequestAsync(httpMessage); 

And it works! Displays ugly system popup over my app asking to enter credentials (it is already filled in - because I have set credentials in the code) - after hitting OK button response status is OK - all worked like a harm, the way it should.

So what is the problem? Problem is when I don't want to show this system popup to users. If only I set:

filter.AllowUI = false;

all magic disappears. No popup, no authentication. Response gives 401 error. Unauthorized.

Why? What am I doing wrong? Is it possible to have NTLM authentication working fine on Windows 10 UWP apps without system popup? Another SDK issue?

I have a solution that is implemented similarly and it does work with NTLM. Do you have the "Enterprise Authentication" and "Internet (Client & Server)" capabilities checked and do you provide the domain with the username?

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