OAuth2.0如何与OData Client Code Generator集成?
How to integrate OAuth2.0 with OData Client Code Generator?
我开发了一个实现 OAuth2.0 和 OData 的 WebApi。
现在我正在创建一个客户端来测试我到目前为止实现的内容。我已经使用 OData 客户端代码生成器生成了 OData 模板,但如何在 OData 请求中引入去访问令牌?
知道如何扩展 OData 模板以引入 OAuth2.0 方案吗?
或者更简单的方法,如何在每个 OData 请求中引入 OAuth 访问令牌?
更新
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);
}
}
如果您想将访问令牌添加为 HTTP 请求header,以下示例应该有所帮助:
var context = new DefaultContainer(new Uri("http://host/service/"));
context.SendingRequest2 += (s, e) => e.RequestMessage.SetHeader("headerName", "headerValue");
在您指定此代码后发送的每个请求都将包含此 header。
经过一些研究,我找到了解决方案:
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);
}
我开发了一个实现 OAuth2.0 和 OData 的 WebApi。
现在我正在创建一个客户端来测试我到目前为止实现的内容。我已经使用 OData 客户端代码生成器生成了 OData 模板,但如何在 OData 请求中引入去访问令牌?
知道如何扩展 OData 模板以引入 OAuth2.0 方案吗? 或者更简单的方法,如何在每个 OData 请求中引入 OAuth 访问令牌?
更新
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);
}
}
如果您想将访问令牌添加为 HTTP 请求header,以下示例应该有所帮助:
var context = new DefaultContainer(new Uri("http://host/service/"));
context.SendingRequest2 += (s, e) => e.RequestMessage.SetHeader("headerName", "headerValue");
在您指定此代码后发送的每个请求都将包含此 header。
经过一些研究,我找到了解决方案:
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);
}