Replacement for PreAuthenticate in Portable Class Libraries

I am moving the library to PCL and have to find a solution to manage the credentials of the HTTP request. I have selected specific credential classes such as CredentialCache (which is not portable) and now only uses ICredentials, so client applications can create the correct credentials and just send a link to the interface.

However, it still needs to be resolved. On some platforms, HttpWebRequest has a neat PreAuthenticate property that provides initial handshaking. Without it, the client must catch and respond to a 401 response. But PreAuthenticate is not part of most PCL profiles, and I'm wondering if there is any permission for this, or if the client will need to implement its own replacement logic (which is stupid, since this is a standard piece of code).

Thank you in advance

+4
source share
3 answers

PreAuthenticate can be set using the HttpClienHandler, which the HttpClient accepts as a parameter constructor, for example:

HttpClientHandler handler = new HttpClientHandler() { UseDefaultCredentials = true, PreAuthenticate = true }; HttpClient client = new HttpClient(handler); 
0
source

You will need to write code to handle this yourself. The API will only be portable if it is available on all platforms that you are targeting. In this case, PreAuthenticate was the new API in the .NET 4.5 and Windows Store applications, so it will not be available in the portable class library if you target other platforms (e.g. .NET 4, Silverlight, or Windows Phone).

+1
source

I have the same problem. I tried to set credentials for one NetworkCredental (user, password), but it does not work in WinRT, although it works in Windoes Phone. So far, the only way to make it work in WinRT is to create a CredentialCache by reflection.

 var credCacheType = Type.GetType("System.Net.CredentialCache, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); var credCache = credCacheType.GetConstructor(new Type[0]).Invoke(new object[0]); var addMethod = credCacheType.GetMethod("Add", new Type[] { typeof(Uri), typeof(string), typeof(NetworkCredential) }); addMethod.Invoke(credCache, new object[] { new Uri(_server), "Digest", Credential }); request.Credentials = (ICredentials)credCache; 
+1
source

All Articles