Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp - Authorization Header not coming across to WCF REST service

I am trying to call a locally hosted WCF REST service over HTTPS with basic auth.

This works and the Authorization header comes thru just fine and all is happy:

ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate; var request = (HttpWebRequest)WebRequest.Create("https://localhost/MyService/MyService.svc/"); request.Method = "GET"; request.ContentType = "application/json"; request.Headers.Add(   System.Net.HttpRequestHeader.Authorization,   "Basic " + this.EncodeBasicAuthenticationCredentials("UserA", "123"));  WebResponse webResponse = request.GetResponse(); using (Stream webStream = webResponse.GetResponseStream()) {     if (webStream != null)     {         using (StreamReader responseReader = new StreamReader(webStream))         {             string response = responseReader.ReadToEnd();         }     } } 

When I try to use RestSharp however, the Authorization header never comes thru on the request:

ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;  var credentials = this.EncodeBasicAuthenticationCredentials("UserA", "123");  var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/");    var restRq = new RestSharp.RestRequest("/"); restRq.Method = Method.GET; restRq.RootElement = "/"; restRq.AddHeader("Authorization", "Basic " + credentials); var restRs = client.Execute(restRq); 

What am i doing wrong with the RestSharp method?

I know that the AddHeader method works because this:

restRq.AddHeader("Rum", "And Coke"); 

will come thru, only "Authorization" seems stripped out/missing.

like image 833
Chris Hawkins Avatar asked Jul 20 '13 12:07

Chris Hawkins


People also ask

How do I request RestSharp?

To make a POST request using RestSharp, you can use the following code: RestRequest request = new RestRequest("Default", Method. POST);

What is HttpBasicAuthenticator?

The HttpBasicAuthenticator allows you pass a username and password as a basic Authorization header using a base64 encoded string.

What is RestSharp client?

Definition of RestSharp RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


2 Answers

instead of adding the header 'manually' do the following:

var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/"); client.Authenticator = new HttpBasicAuthenticator("UserA", "123"); 
like image 72
wal Avatar answered Sep 20 '22 11:09

wal


I used milano's answer to get my REST service call to work (using GET)

    Dim client2 As RestClient = New RestClient("https://api.clever.com")      Dim request2 As RestRequest = New RestRequest("me", Method.GET)      request2.AddParameter("Authorization", "Bearer " & j.access_token, ParameterType.HttpHeader)      Dim response2 As IRestResponse = client2.Execute(request2)     Response.Write("** " & response2.StatusCode & "|" & response2.Content & " **") 

The key was making sure there was a space after the word 'Bearer' but this may apply to any type of custom token authorization header

like image 40
j.hull Avatar answered Sep 18 '22 11:09

j.hull