当前位置:网站首页>Pay attention to the request forwarding problem of. Net core
Pay attention to the request forwarding problem of. Net core
2020-11-09 11:37:00 【osc_4otxquc2】
【 Reading guide 】 In recent days, , I have a little buddy who cares about my official account , I have a problem that I haven't solved for a long time , This problem is of referential significance , Here I'd like to share with you , I hope it can help you
The Intranet environment is isolated from the extranet , Now all requests from the Internet need a dedicated server to be transferred to the intranet , use app.UseRewriter transfer , When forwarding from Internet server to intranet server Header Inside Authorization I lost it , To reset RewriteContext.HttpContex Header Not good either. , Is there any way to solve ?
My idea at the time was , Really not line , In the extranet will token Put directly url or body It's done in here , In this case , Every time an external network adds an interface , All have to be token Take it out and convert it , Intranet access in the same way , This is what little friends can't stand .
Forward questions
Here we create two Web Applications , Then add custom forwarding rules . First of all, we are in the first Web The application creates forwarding rules for the following interface requests
public class RewriteForwardRules
{
public static void RedirectRequests(RewriteContext context)
{
var request = context.HttpContext.Request;
if (request.Path.Value.StartsWith("/api/forward", StringComparison.OrdinalIgnoreCase))
{
var response = context.HttpContext.Response;
response.Headers[HeaderNames.Location] = "http://localhost:8091/api/custom";
context.Result = RuleResult.EndResponse;
}
}
}
And then in startup Inject our custom forwarding rules
app.UseRewriter(new RewriteOptions().Add(RewriteForwardRules.RedirectRequests));
Of course , If URL(GET request ) or Body(POST request ) Contains other parameters , Write its corresponding forwarding to URL or Body that will do , here token Already stored in the request header , So we can forward the request directly
Next we pass Postman The analog Internet sends out the following POST request
Then , We're in the second Web To receive forwarding requests in the application , And get the token Information
[HttpPost]
public IActionResult Custom()
{
var token = Request.Headers[HeaderNames.Authorization].ToString();
return Ok(token);
}
And then as soon as we run , The results are not forwarded to the corresponding intranet application , Why is that ?
Status code (308) Set up
in fact , Forwarding a request involves reallocation of resources to another URL problem , Of course, what we need to pay attention to is , Since it's a forwarding request , It is inevitable that the forwarding and receiving methods must be consistent , Otherwise, it must not be . So we have to explicitly specify the redirection status code , Set to 308, as follows :
For status codes 308 It means , We can see .NET Core State code enumeration is explained in : Permanent redirection , The original request mode must be consistent with the target request mode , Support both original request and target request as GET or POST.
.NET Core The explanation of this status code in is not so detailed , We're here to explain the status code (https://httpstatuses.com/308), Here I post Google translated Chinese
308 Permanent redirection : A new permanent has been assigned to the target resource URI, Any future reference to the resource should use one of these URI.
The client with link editing function should automatically send a valid request to URI Re link references from the server to one or more new references sent by the server .
The server should generate a Location Header fields , It contains a new permanent URI The first choice of URI quote . User agents can use location field values for automatic redirection . The response payload of the server usually contains a short hypertext comment , It includes pointing to new URI Hyperlinks for .
By default ,308 The response can be cached ; namely , Unless method definition or explicit cache control
Status code (301) Set up
We can also specify the response status code as 301,
response.StatusCode = 301;
Of course, at this time, the internal network receiving program must be changed to GET, as follows :
301 A permanent move : A new permanent has been assigned to the target resource URI, Any future reference to the resource should use one of these URI.
So the status code 301 and 308 What's the difference ?301 similar 308 A permanent move , It's just ,301 It is not allowed to change the request method from GET Change to POST
???? Please set the status code to 301 or 308
???? 301 similar 308 A permanent move , It's just ,301 It is not allowed to change the request method from GET Change to POST
???? Based on the above , The status code is recommended for request forwarding 308
版权声明
本文为[osc_4otxquc2]所创,转载请带上原文链接,感谢
边栏推荐
- [design pattern] Chapter 4: Builder mode is not so difficult
- JT-day09
- Understanding task and async await
- 基于synchronized锁的深度解析
- 商品管理系统——商品新增本地保存实现部分
- Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters
- In the future, China Telecom will make cloud computing service the main business of China Telecom
- For and for... In, for each and map and for of
- Handwritten digital image recognition convolution neural network
- Interface tests how to pass files in post requests
猜你喜欢
手写数字图片识别-卷积神经网络
块级元素和行内元素
Python零基础入门教程(01)
How to query by page after 10 billion level data is divided into tables?
商品管理系统——商品新增本地保存实现部分
Recommended tools for Mac
1486. 数组异或操作
What details does C + + improve on the basis of C
解决python调用 ffmpeg时 ‘ffmpeg‘ 不是内部或外部命令,也不是可运行的程序
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
随机推荐
手写Koa.js源码
从编码、网络传输、架构设计揭秘腾讯云高质量、高可用实时音视频技术实践...
How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)
Principle analysis and performance tuning of elasticsearch
A simple ability determines whether you will learn!
GitHub 上适合新手的开源项目(Python 篇)
配置交换机Trunk接口流量本地优先转发(集群/堆叠)
Open source projects for beginners on GitHub (Python)
Configure switch trunk interface traffic local priority forwarding (cluster / stack)
Jsliang job series - 08 - handwritten promise
Handwriting Koa.js Source code
用一种简单的方式实现终端文字粘贴板
Chrome浏览器引擎 Blink & V8
As a user, you can't get rid of the portrait!
块级元素和行内元素
医疗项目管理的三种实用技巧
Mapstructure detoxifies object mapping
SQL语句实现水仙花数求取
Learning notes of nodejs
When Python calls ffmpeg, 'ffmpeg' is not an internal or external command, nor a runnable program