请求中继webservice未登录

时间:2016-03-18 04:05:59

标签: c# asp.net asp.net-mvc-4 authentication

我已经创建了这个Web服务,它将请求转发给另一台服务器。它需要对其自己的URL进行请求,然后将请求发送到不同的URL并将结果返回给它的客户端。

void Application_BeginRequest(object sender, EventArgs e)
{
    // Setup destination url schemes
    string newAuth = "localhost:1861";
    string newUrl = "http://" + newAuth + Request.Url.PathAndQuery;

    // Setup the request from this server to the other server
    HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(newUrl);
    newRequest.AllowAutoRedirect = false;

    // Copy all needed headers
    List<string> copyHeaderNames = new List<string>()
    {
        "Accept-Encoding",
        "Accept-Language",
        "Upgrade-Insecure-Requests",
        "Cache-Control",
        "Connection",
        "Cookie"
    };
    foreach (var key in copyHeaderNames)
    {
        try
        {
            if (newRequest.Headers.AllKeys.Contains(key))
            {
                newRequest.Headers[key] = Request.Headers[key].Replace(Request.Url.Authority, newAuth);
            }
            else
            {
                newRequest.Headers.Add(key, Request.Headers[key].Replace(Request.Url.Authority, newAuth));
            }
        }
        catch { }
    }

    // Then setup the constant paramenters of the new request
    newRequest.KeepAlive = Request.Headers["Connection"] == "keep-alive";
    newRequest.Accept = Request.Headers["Accept"];
    newRequest.Expect = Request.Headers["Expect"];
    newRequest.UserAgent = Request.Headers["User-Agent"];

    newRequest.ContentType = Request.ContentType;
    newRequest.Method = Request.HttpMethod;
    newRequest.Host = newAuth;
    newRequest.Referer = newUrl;

    // If the request is a POST, I need to copy the inputstream.
    if (Request.HttpMethod == "POST")
    {
        byte[] inputBytes = ReadToByteArray(Request.InputStream);
        string inputString = System.Text.Encoding.Default.GetString(inputBytes);

        // Replace original url with destination url
        inputString = inputString.Replace(Request.Url.Authority, newAuth);
        inputBytes = System.Text.Encoding.Default.GetBytes();

        Stream reqStream = newRequest.GetRequestStream();
        reqStream.Write(inputBytes, 0, inputBytes.Length);
        reqStream.Close();
    }

    // Then do the request
    using (var resp = (HttpWebResponse)newRequest.GetResponse())
    {
        // Setup response paramenters
        Response.StatusCode = (int)resp.StatusCode;
        Response.StatusDescription = resp.StatusDescription;

        // Get the response stream
        using (var respstream = resp.GetResponseStream())
        {
            var res = ReadToByteArray(respstream);

            // And respond it in the current response
            Response.BinaryWrite(res);

            // Then I copy all response headers to the current response
            foreach (var key in resp.Headers.AllKeys)
            {
                try
                {
                    // Replace the destination url back to the current url                         
                    string value = resp.Headers[key].Replace(newAuth, Request.Url.Authority);

                    if (Response.Headers.AllKeys.Contains(key))
                    {
                        Response.Headers[key] = value;
                    }
                    else
                    {
                        Response.Headers.Add(key, value);
                    }
                }
                catch { }
            }
        }
    }

    // Tell the program to end the request.
    Response.End();
}

public static byte[] ReadToByteArray(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

现在一切正常,除了登录。另一个网站是asp.net mvc4应用程序,它使用带有身份验证cookie的标准成员资格。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

猜测复制请求对象的内容并不完全复制它。解决方案是沿着OSI模型向下一层到TCP层并在该级别上进行中继。