I've developed a WebApi that implements OAuth2.0 and OData.
Now I'm making a client to test what I've implemented so far. I've generated the templates for OData using the OData Client Code Generator, but how can I introduce de access token in the OData request?
Any idea how to extend the OData templates to introduce the OAuth2.0 scheme? Or a more simpler way, how do I introduce OAuth access token in every OData request?
static void Main(string[] args)
{
var container = new Default.Container(new Uri(baseurl));
TokenResponse accessToken = null;
try
{
accessToken = GetToken();
}
catch (Exception ex)
{
Console.WriteLine("Can't do nothing without an access token...");
return;
}
//I want to introduce in every request the following information:
//Basic autentication header with cliendId + clientSecret
//Access token
//How do I introduce them before making the call on the OData service?
foreach (var s in container.ServiceSessions)
{
string format = ";
Console.WriteLine("PKID:{0}", s.PKID);
}
}
After a bit of research I've found the solution:
var container = new Default.Container(new Uri(http://localhost:9000/));
//Registering the handle to the BuildingRequest event.
container.BuildingRequest += (sender, e) => OnBuildingRequest(sender, e, accessToken);
//Every time a OData request is build it adds an Authorization Header with the acesstoken
private static void OnBuildingRequest(object sender, BuildingRequestEventArgs e, TokenResponse token)
{
e.Headers.Add("Authorization", "Bearer " + token.AccessToken);
}