Using Default Credentials in System.Net.HttpClient request in UWP

為{幸葍}努か 提交于 2019-12-10 10:29:26

问题


We are porting our Windows 8.1 app to UWP and are experiencing an issue with sending default user credentials for single sign-on. It seems that credentials are not being set, despite setting UseDefaultCredentials = true on the handler. This exact code was working in Windows 8.1.

using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
{
    // client._handle.Credentials is null
    // This call fails authentication with a 401 
    // and Fiddler shows no attempt to pass authentication
    var response = await client.GetStringAsync(new Uri(GlobalConfig.Config.BaseUri, "Presenter/SingleSignOn"));
    ...
}

As noted above, in debugging I can see that client._handle.Credentials is null.

I have also tried setting credentials from the System.Net.CredentialCache, but these appear to return empty credentials and they also result in a null client._handle.Credentials.

var handler = new HttpClientHandler() { Credentials = System.Net.CredentialCache.DefaultCredentials };
var handler = new HttpClientHandler() { Credentials = System.Net.CredentialCache.DefaultNetworkCredentials };

I have double-checked our declared Capabilities as per this answer and they look fine. We have declared capabilities:

  • Enterprise Authentication
  • Internet (client)
  • Private Networks (Client & Server)
  • Removable Storage

I have tried using Windows.Web.HttpClient, but it has the same issue--it doesn't find default credentials, so it prompts via a UI. Am I missing something obvious? Or something non-obvious?

TL;DR - I am having trouble passing default user credentials in HttpClient requests.

Edit: I should add that authentication in general is working. If I pass a username/password explicitly, then it works. Obviously, that's not the goal here.

来源:https://stackoverflow.com/questions/44396390/using-default-credentials-in-system-net-httpclient-request-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!