【发布时间】:2016-09-14 23:23:43
【问题描述】:
我有一个代理控制器来重定向 Ajax 请求并将相同的 cookie 从当前域传递到 Web API 端点,但它没有按我的预期工作。例如“https://www.example.com”中的 cookie,Web API URL “https://api.example.com/xyz/abc/”。我想要做的是向
发送一个 Ajax 请求“https://www.example.com/api/proxy/something”
并希望它被重定向到
“https://api.example.com/xyz/abc/something”具有相同的设置(尤其是 cookie)。
这是网站中的 API 控制器:
public class ProxyController : ApiController
{
private string _baseUri = "https://api.example.com/xyz/abc/";
[AcceptVerbs(Http.Get, Http.Head, Http.MkCol, Http.Post, Http.Put)]
public async Task<HttpResponseMessage> Proxy()
{
using (HttpClient http = new HttpClient())
{
string proxyURL = this.Request.RequestUri.AbsolutePath;
int indexOfProxy = proxyURL.IndexOf("proxy/") + 6;
_baseUri = _baseUri + proxyURL.Substring(indexOfProxy, proxyURL.Length - indexOfProxy);
this.Request.RequestUri = new Uri(_baseUri);
//For some reason Ajax request sets Content in the Get requests, because
//of that it was denied complaining about "cannot send a content-body"
if (this.Request.Method == HttpMethod.Get)
{
this.Request.Content = null;
}
return await http.SendAsync(this.Request);
}
}
}
它不会重定向请求。在响应中,请求的 URL 与原始请求相同。请求标头中的主机是“www.example.com”而不是“api.example.com”,最近几天我一直在为这个问题发疯。
【问题讨论】:
标签: c# ajax cookies asp.net-web-api proxy