Network Credentials not getting passed in RestSharp conole app

1k Views Asked by At

My network credentials are not getting passed from a RestSharp application to my web application.

This is an application that will be called from a scheduled task with network service credentials.

If I use the CredentialCache.DefaultNetworkCredential, I get a 401 error. If I create a new NetworkCredential object using my explicit username / password, everything works correctly. So I am fairly confident that the application should work, once I get this credential problem figured out.

Is there a special process for setting the credentials in RestSharp?

 RestSharp.RestClient c = new RestClient(args[0]); // url from command line.

 var req = new RestRequest(Method.POST);
 req.RequestFormat = DataFormat.Json;
 //NetworkCredential cred = new NetworkCredential("myUserName", "myPassword"); // Works
 NetworkCredential cred = CredentialCache.DefaultNetworkCredentials; // not working
 req.UseDefaultCredentials = true; // used with DefaultNetworkCredentials

 req.Credentials = cred; // set credentials
 req.AddJsonBody(args[1]); // append the JSON body to the POST

 var response = c.Execute(req);  // get the response
 var content = response.Content; // put in a var for some debugging
 System.Console.WriteLine(content); // write out the response for logging

Is there a different method that I would need to use to set the credentials in RestSharp?

1

There are 1 best solutions below

0
dadkind On

After viewing some Fiddler traces, I see that when using DefaultNetworkCredentials the Authentication Header is not getting set.

When I use explicit credentials ("myUsername", "myPassword") the authentication header gets set and the username/password are passed to the web service.

I can explicitly set the Authentication header, but again, if I use the DefaultNetworkCredential to pass the username/password the call fails because the username/password are empty.

So my problem seems to be that DefaultNetworkCredentials are empty at runtime. I will post that question separately.