How to do a preemptive authentication with c#?

4.8k views Asked by At

Our clients have a Java Web Service that we want to cosume from c#.net application, but when i try to consume or call any method i get an Internal Error message. This web service needs an username and password to authenticate trough the server (where the WS is hosted).

I test the web service with Soap UI and I get a response from the java WS but only if I choose Preemptive Autorizathion option. When I choose another option, I get an error, as I wrote above.

The WS were developed in Java, while our application is developed in .NET Framework 2.0, so the question is: is there any way to authtenticate with preemptive authorization in c#?

If it helps, the WS is hosted on a HTTPS site.

Here is some code that i'm using to authenticate to the server.

NetworkCredential netCredential = new NetworkCredential(username, password); 
Uri uri = new Uri(service.Url);
ICredentials credentials = netCredential.GetCredential(uri, "Basic");
service.Credentials = credentials;
service.PreAuthenticate = true;

Also i tried with this code:

string usernamePassword = username + ":" + password;
CredentialCache mycache = new CredentialCache();
WebRequest myReq = WebRequest.Create(s.Url);
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes((usernamePassword))));
mycache.Add(new Uri(service.Url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
service.Credentials = myReq.Credentials;
service.UserAgent = ".net FrameWork";  

Do you have any idea how to solve this problem?

0

There are 0 answers