ahsteele ahsteele - 23 days ago 10
C# Question

How do I pass credentials with DalSoft.RestClient?

I am attempting to use DalSoft.RestClient to make restful calls to an internal service which requires network credentials (for my use case a default credential) be provided.

The constructor for

RestClient
provides an overload to pass in an
IHttpClientWrapper
which I could implement handling credentials, but am hoping there's an out of the box solution for passing credentials to
RestClient
.

How do I pass credentials to the
DalSoft.RestClient
?

Answer

For any credentials that are set via a header such as basic or oauth you can use DefaultHeaders. Example for oauth2 bearer token:

dynamic client = new RestClient("http://localhost/");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + bearerToken);

If you are talking about kerberos or ntlm at the moment there is no method to do this but as you suggested you can implement IHttpClientWrapper to do this. Strangely Credentials are passed to a HttpClient using a HttpClientHandler. Below is an example of how to do this:

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential();
HttpClient client = new HttpClient(handler);

I realize implementing IHttpClientWrapper just to do this isn't ideal, so if you need this functionality I'll look at adding it to the ctor. It would look like this:

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential();
new RestClient("http://localhost/", handler); 

Update this is now supported as of 2.0.3

Comments