SecondBrain/Ralf @ Ralf Koop/Programmieren/00_C# HttpClient integrated authentication.md
Ralf Koop e138992abe .
2022-12-21 12:30:21 +01:00

1.4 KiB

C# HttpClient integrated authentication

Montag, 5. Februar 2018

16:04

 

Geek With Opinions

C# HttpClient integrated authentication

July 14, 2014

HttpClient has become the standard way to make http requests in C#. This is mainly used for API calls but has other uses as well. Recently, I have had to make http requests to servers that require authentication and the documentation on how to do this is scattered. The funny part is that it is really easy to do.

Var uri =newUri("<url>");

Var credentialCache =newCredentialCache();

credentialCache.Add(

newUri(uri.GetLeftPart(UriPartial.Authority)),

"<auth method>",

newNetworkCredential("<user name>","<password>","<domain>")

);

HttpClientHandlerhandler =new HttpClientHandler();

handler.Credentials = credentialCache;

Var httpClient = new HttpClient(handler );

Var response = httpClient.GetAsync(uri).Result;

 

 

can be basic, digest, ntlm, or negotiate. Then just updated the Network Credentials to that of the user you want to make the call and you are good to go.

It appears that kerberos on its own does not work. This may be because of my server configuration. However if you use negotiate, HttpClient will use kerberos if the server is configured for it otherwise it will fallback to NTLM.

 

Aus <https://www.geekwithopinions.com/2014/07/14/c-httpclient-integrated-authentication/>