CarlosTI
CarlosTI

Reputation: 133

Assign CredentialCache object to a HttpWebRequest.Credentials in Windows Universal Apps don't work

I'm creating Windows Universal App, with Visual Studio 2015, Framework 4.5.2. Target version 10586, Minimum Version 10240.

My App need authenticate against https page with Credentials. I've read a lot examples like this:

WebRequest req =     HttpWebRequest.Create("https://intranet.anyweb.com");
NetworkCredential ntCred = new NetworkCredential(user, pass);
req.Credentials = ntCred;
CredentialCache cacheCred = new CredentialCache();
cacheCred.Add(new Uri("https://intranet.anyweb.com"), "NTLM", ntCred);
req.Credentials = cacheCred;
req.Method = "GET";
var resp = (HttpWebResponse)req.GetResponseAsync();

This example works fine in desktop app, but when I try to make the same in my Universal APPs, and excute GetResponseAsync, thrown this error:

"The value 'System.Net.CredentialCache' is not supported for property 'Credentials'."

Same code in an standard desktop windows app works fine, so why I can't assign a CredentialCache object to HttpWebRequest credentials in my Universal App?

Upvotes: 1

Views: 796

Answers (1)

ndelabarre
ndelabarre

Reputation: 269

I encourage you to read the following Microsot blog post where you'll learn that System.Net.Http.HttpClient and Windows.Web.Http.HttpClient are the recommended APIs for universal apps over older discouraged APIs such as WebClient and HttpWebRequest.

Using the following code, you should be able to use user credentials :

HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.ServerCredential = new PasswordCredential( uri, username, password);
HttpClient httpClient = new HttpClient(filter);

HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Get;
request.RequestUri = new Uri(uri, UriKind.Absolute);

HttpResponseMessage response = await httpClient.SendRequestAsync(request);

If the server requests NTLM authentication, the HTTP stack of the OS will perform the authentication with those credentials. For apps with enterprise capability, the Windows logon credentials of the user will be used if no credentials are set on the ServerCredential property. For other apps, by default, UI will pop up asking for user credentials

Upvotes: 1

Related Questions