【问题标题】:Web API2 proxy doesn't redirect the requestsWeb API2 代理不重定向请求
【发布时间】: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


    【解决方案1】:

    我两天前就遇到过这种问题。 还没弄清楚到底是什么原因造成的,我今天要调查一下,让你知道。

    但在那之前,您可以尝试使用 RestSharp,它解决了我的问题。

    我认为这是 HttpClient 的 BaseAddress 属性的问题,它以某种方式使用初始地址进行初始化,并向该地址而不是代理地址发出请求。

    我会调查并通知你。

    【讨论】:

    • 听起来我们在同一条船上 :),实际上我在考虑 RestSharp 但我没有为此尝试过。我将使用 RestSharp,希望它能修复它,仍然很好奇为什么 HttpClient 不起作用。谢谢
    【解决方案2】:

    HttpClient 可以正常工作。我们已经多次这样做了。但是,cookie 很棘手,因为它们与域相关联。下面是一些没有 cookie 的代理代码,可以帮助您入门。

    [AcceptVerbs(Http.Get, Http.Head, Http.MkCol, Http.Post, Http.Put)]
    public async Task<HttpResponseMessage> Proxy()
    {
        var request = this.Request;
    
        var proxyUri = this.GetProxyUri(request.RequestUri);
    
        request.RequestUri = proxyUri;
        request.Headers.Host = proxyUri.Host;
        if (request.Method == HttpMethod.Get)
        {
            request.Content = null;
        }
    
        //todo: Clone all cookies with the domain set to the domain of the proxyUri. Remove the old cookies and add the clones.
    
        using (var client = new HttpClient())
        {
            //default is 60 seconds or so
            client.Timeout = TimeSpan.FromMinutes(5);
    
            return await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);
        }    
    }
    
    private string _baseUri = "https://api.example.com/xyz/abc/";
    
    private Uri GetProxyUri(Uri originalUri)
    {
        var proxyUri = originalUri.AbsolutePath;
        var indexOfProxy = proxyUri.IndexOf("proxy/") + 6;
        var finalUri = _baseUri + proxyUri.Substring(indexOfProxy, proxyUri.Length - indexOfProxy);
        return new Uri(finalUri);
    }
    

    由于域切换,您可能无法让 cookie 正常工作。您的客户端和服务器将仅限于其域。如果您拥有代理目标,您可能需要更改它以允许除 cookie 之外的其他机制。您可以使用标题或查询字符串等吗?域名可能是个杀手。

    【讨论】:

    • 只是想更多地考虑 cookie 和域。在您的示例中,顶级域是相同的,只是不同的子域。如果是这种情况,只需确保 cookie 设置器使用跨域 cookie。这样,cookie 将自动传输。如果您完全位于不同的域中,我认为 cookie 对您不起作用。
    • 感谢 ManOVision!是的,cookie 是为“example.com”设置的,它应该可以工作。
    • 这在重定向到 api.example.com 时有效,但是“example.com”的 cookie 不会自动传输,我还需要明确设置吗?
    • 我必须明确设置 cookie,一切正常!!感谢 ManOVision!
    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多