How To Fix Web Service Negotiate Login Errors Using C#


Have you ever had an issue calling a web service due to the fact that you need to provide credentials to the service?  The below example calls a web service that has an API level user / pass, and also a network user / pass.



ServiceApi.API api = new ServiceApi.API();

//API Credentials
ServiceCredentials credentials = new ServiceCredentials();
credentials.Username = "user";
credentials.Password = "password";

//Creds, for 401. challenge
System.Net.CredentialCache cc = new System.Net.CredentialCache();
System.Net.NetworkCredential creds = new System.Net.NetworkCredential("user", "pass", "domain");
cc.Add(new Uri("https://webservice.url"), "Negotiate", creds);
api.Credentials = creds;
string result = api.GetResultscredentials);



Replace the first user / pass with your web service password.  Replace the second set of user / password with your network user / password.