Up:: OData (Open Data Protocol)

Basic Authentication with OData

Learn by elaboration

Using the request headers directly

Another option is to just create and send the authentication header yourself.

  1. Hook up to the DataServiceContext’s SendingRequest Event:
ctx.SendingRequest +=new EventHandler<SendingRequestEventArgs>(OnSendingRequest);
  1. Add the Basic Authentication Header to the request:
static void OnSendingRequest(object sender, SendingRequestEventArgs e)  
{  
  var creds = “user” +:+ “password”;  
  var bcreds = Encoding.ASCII.GetBytes(creds);  
  var base64Creds = Convert.ToBase64String(bcreds);   
  e.RequestHeader.Add(“Authorization”, “Basic ” + base64Creds);     
}

As you can see this is pretty simple. And has the advantage that it will work even if the server doesn’t respond with a challenge (i.e. WWW-Authenticate header).


📇Additional Metadata