Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HTTP header to propagate in ASP.NET Core

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use HTTP headers to spread in ASP.NET Core". In daily operation, I believe many people have doubts about how to use HTTP headers in ASP.NET Core. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use HTTP headers to spread in ASP.NET Core". Next, please follow the editor to study!

Demo

Next, let's create two Web API projects, ServerA and ServiceB, to demonstrate this functionality.

First, ServiceB has a GET method that returns all headers received in the request, which enables us to verify that the headers are propagated:

Public IHeaderDictionary Get () {return Request.Headers;}

To execute the GET request of ServiceB, we can respond similar to the following figure:

Now, call ServiceB with ServiceA and return a response.

This will show us which headers propagate to ServiceB:

Public async Task Get () {var client = _ clientFactory.CreateClient ("ServiceB-Client"); var response = await client.GetAsync ("/ ServiceB"); return await response.Content.ReadAsStringAsync ();}

Executing the GET request for ServerA, you can see that the Authorization header is not propagated:

At this point, we can reference the Nuget package Microsoft.AspNetCore.HeaderPropagation and modify Startup.cs to enable header propagation:

Public void ConfigureServices (IServiceCollection services) {/ / defines the header services.AddHeaderPropagation (options = > options.Headers.Add ("Authorization")) that needs to be propagated; services.AddHttpClient ("ServiceB-Client", options = > options.BaseAddress = new Uri) ("http://localhost:57516"))// definition enables header propagation. AddHeaderPropagation ();.} public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {/ / add header propagation middlewareapp.UseHeaderPropagation ();.}

Execute ServerA's GET request again, and you can see that the Authorization header is propagated correctly:

At this point, the study on "how to use HTTP headers to spread in ASP.NET Core" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report