我已经使用DelegatingHandler
作为基类创建了反向代理。它将用于将请求从我们的MVC Web应用程序发送到在我们的本地域中运行的供应商的REST服务。
为了验证请求没有被篡改,我们需要检查一些变量并将它们与Session中存储的值进行比较。
但是,似乎与此相关的HttpRequestMessage request
和HttpContextBase
都没有可用的会话变量。
RouteTable.Routes.MapHttpRoute(
name: "ProxyRoute",
routeTemplate: "/api/proxy/{*path}",
handler: HttpClientFactory.CreatePipeline(
innerHandler: new HttpClientHandler(),
handlers: new DelegatingHandler[] { new ProxyHandler() }),
defaults: null,
constraints: null
);
public class CheckoutProxyHandler : DelegatingHandler
{
private static HttpClient client = new HttpClient();
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
//Modify some headers here, change Uri...
var session = ???
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
return response;
}
}
有什么办法可以迫使Asp.Net PipeLine在DelegatingHandler内部提供一个(只读)会话状态?