简体   繁体   English

简单 Odata 客户端 - 如何在每个请求标头中添加 oAuth 令牌?

[英]Simple Odata Client - How to add oAuth Token in each request header?

In Microsoft oData v4 proxy client, there is an option to add auth token into each request.在 Microsoft oData v4 代理客户端中,有一个选项可以将身份验证令牌添加到每个请求中。 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);
}

How can I do the same using simple odata client?如何使用简单的 odata 客户端做同样的事情?

Apparently I should provide an explanation of why this is the answer.显然,我应该解释为什么这是答案。

Explanation: this is how you add the token for Simple ODataClient.说明:这是为 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);

Instead of using the delegate method to intercept and add the Authorization header on every Http call, a clearer/cleaner solution is to instantiate the ODataClient with an HttpClient instance.一个更清晰/更简洁的解决方案是使用 HttpClient 实例实例化 ODataClient,而不是使用委托方法在每个 Http 调用中拦截和添加 Authorization 标头。

This also allows you to control the HttpClient lifecycle and re-use it ( as you should be doing normally anyway! ) otherwise ODataClient will create a new HttpClient instance on every call and that is just inefficient and causes lots of churn on the socket layer.这也允许您控制 HttpClient 生命周期并重新使用它(无论如何您应该正常做!)否则 ODataClient 将在每次调用时创建一个新的 HttpClient 实例,这只是低效并导致套接字层上的大量流失。 Not normally a problem but can be on high volume code so just a good habit.通常不是问题,但可以使用大量代码,所以只是一个好习惯。

The code below is an extract of a .Net core app using an Azure AD OAuth2 token to connect to a Dynamics 365 OData Web API.下面的代码是使用 Azure AD OAuth2 令牌连接到 Dynamics 365 OData Web API 的 .Net 核心应用程序的摘录。

        httpClient.BaseAddress = new Uri(yourODataServiceRootURL);
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", yourBearerAccessToken);

        //Use the httpClient we setup with the Bearer token header
        var odataSettings = new ODataClientSettings(httpClient, new Uri("api/data/v9.1", UriKind.Relative));

        var odataClient = new ODataClient(odataSettings);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OData Simple.OData.Client V3发送其他请求 - OData Simple.OData.Client V3 sends other request 如何通过使用身份验证筛选器将标头设置为令牌请求OAuth? - How to set header as token request OAuth by using authentication filter? 简单的OAuth2身份验证-请求访问令牌 - Simple OAuth2 authentication - Request an access token 编写一个简单的OData客户端:如何查询服务? - Writing a simple OData client: how to query service? 如何在Simple OData Client中启用gzip压缩? - How to enable gzip compression in Simple OData Client? 如何使用C#为Simple.OData.Client创建条目请求设置主体? - How to set the body for a Simple.OData.Client create entry request using C#? 如何在C#中的GraphQL Client端点的请求主体中发送oauth_token和client_id - How to send oauth_token and client_id in the request body of GraphQL Client endpoint in c# 如何在HTTP请求的标头中添加承载令牌? - How do I add a Bearer Token to the header of a HTTP request? 如何将安全令牌添加到对WCF服务发出的请求的标头中? - How to add a security token to the header of a request made to WCF service? 简单的OData Client 4.0:将自定义标头添加到内部批处理请求中 - Simple OData Client 4.0: Add custom headers to internal batch requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM