I was trying to implement a client for a odata service by following this article
Challanges that I had:
Following the steps in the article, I figured that
1) the (ProductClient).odata.config was not auto generated - we could create one though
2) Client that requires credentials to access the endpoint (well in my case it was basic auth)
3) Most important- couldnt find a related article on stackoverflow as well :)
Have posted the solution below for newbies like me!
Solution
In order to achieve something similar to what is mentioned in the article (ie)
accessing strongly typed Odata entities via Odata endpoint that requires authentication -
static void ReadingODataEndPointByPassingMyBasicAuthCreds() {
// e.g. URL = http://localhost/myApi/odata
var url = ConfigurationManager.AppSettings["MyAPIBaseUrl"];
var container = new MyApi.Container(new Uri(url));
container.SendingRequest2 += SendBaseAuthCredsOnTheRequest;
foreach(var myEntity in container.MyEntities) {
Console.WriteLine(myEntity.Name);
Console.Write(string.Format("Description: {0}", myEntity.Description));
}
Console.Read();
}
private static void SendBaseAuthCredsOnTheRequest(object sender,
System.Data.Services.Client.SendingRequest2EventArgs e) {
var authHeaderValue = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", ConfigurationManager.AppSettings["username"]
, ConfigurationManager.AppSettings["password"])));
//this is where you pass the creds.
e.RequestMessage.SetHeader("Authorization", "Basic " + authHeaderValue);
}