简单的 Odata 客户端 - 如何在每个请求标头中添加 oAuth 令牌? [英] Simple Odata Client - How to add oAuth Token in each request header?
本文介绍了简单的 Odata 客户端 - 如何在每个请求标头中添加 oAuth 令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Microsoft oData v4 代理客户端中,可以选择将身份验证令牌添加到每个请求中.可以通过以下方式实现:
In Microsoft oData v4 proxy client, there is an option to add auth token into each request. It can be achieved by following way:
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);
}
如何使用简单的 odata 客户端执行相同操作?
How can I do the same using simple odata client?
推荐答案
显然我应该解释为什么这是答案.
Apparently I should provide an explanation of why this is the answer.
说明:这是为 Simple ODataClient 添加令牌的方式.
Explanation: this is how you add the token for Simple ODataClient.
var settings = new ODataClientSettings(new Uri("http://localhost:9000/"));
settings.BeforeRequest += delegate(HttpRequestMessage message)
{
message.Headers.Add("Authorization", "Bearer " + token.AccessToken);
};
var client = new ODataClient(settings);
这篇关于简单的 Odata 客户端 - 如何在每个请求标头中添加 oAuth 令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文