I have a working Python script which sends a request to an web service using Windows Authentication in order to get a token.
The bit that I’m trying to replicate is:
url = 'https://ssologin.company.com/Logon/Logon.aspx?permission=permission' h = WC.Dispatch('WinHTTP.WinHTTPRequest.5.1') h.SetAutoLogonPolicy(0) h.SetOption(6, False) h.Open('GET', url, False) h.Send()
I have an ASP.NET Core service which tries to do it this way:
var uri = new Uri("https://ssologin.company.com/Logon/Logon.aspx?permission=permission"); WinHttpHandler handler = new WinHttpHandler(); handler.ServerCredentials = CredentialCache.DefaultNetworkCredentials; HttpClient httpClient = new HttpClient(handler); var httpResponseMessage = await httpClient.GetAsync(uri);
Right now I am getting the following error: WinHttpException: Error 12029 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, ‘A connection with the server could not be established’.
However, if I comment // handler.ServerCredentials = CredentialCache.DefaultNetworkCredentials;
, then I get Error 401 Unauthorised. I also noticed that CredentialCache.DefaultNetworkCredentials is NULL.
Can you please let me know what I am missing here? Thanks.
Advertisement
Answer
Actually, the authentication worked fine, but the request was redirecting to a service which refuses to connect. The easy fix to this was to just disable redirecting. Also, at @spzvtbg’s suggestion, I used the default HttpClienthandler
.
The final code ended up like this:
var uri = new Uri("https://ssologin.company.com/Logon/Logon.aspx?permission=permission"); HttpClientHandler handler = new HttpClientHandler(); handler.UseDefaultCredentials = true; handler.PreAuthenticate = true; handler.AllowAutoRedirect = false; HttpClient httpClient = new HttpClient(handler); var httpResponseMessage = await httpClient.GetAsync(uri);