代码改变世界

POST跨域解决方案

2013-06-14 11:51  时空印记  阅读(1260)  评论(0编辑  收藏  举报

       最近,公司的WEB应用越来越多,总是要进入不同的网址去打开不同的WEB应用,总是显得麻烦。于是打算做一个公共的登陆页面,根据选择不同的应用登陆到对应的WEB里面。

      听起来似乎很容易,也可以用很多种方式,可以是共享SESSION、利用COOKIE、GET OR POST以及SQL SERVER 等等等。 GET首先因为安全问题不用考虑,因为某些原因我选择了POST 方式来进行LOGIN。我使用双重验证,在公共登陆页面,进行验证一次,如果无误,再加密POST到相应的WEB应用程序,WEB应用接收到数据后,再进行一次比对验证。加密方式可以选择公钥私钥或者MD5,就算POST被截取了,也没很明显的安全隐患。

      利用GOOGLE 搜索得到以下几种方案:

      1. Response.Redirect:这种方法最普遍的使用,但它只能使用GET形式 ,是没有办法做到POST的。

      2.Server.Transfer:这种方式是POST非GET,但是遗憾的是这种方式只能在同一个应用程序里面进行,如果向第三方应用程序发送一个请求,这也将是无法正常工作的。

      3. AJAX POST:ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作。有人说可以利用Access-Control-Allow-Origin解决,反正我是没成功。

      4. HTML5 postMessage:很好,HTML5解决了这个问题,但是不是所有的浏览器都很好的支持了HTML5的。

      5.HttpWebRequest 和 WebClient:前面数据或转换成流或添加到集合到POST,一切都很完美。但是两者一样最后都给你返回一个404 错误,找不到页面。为什么?默认postback总是页面本身。

       以上均未成功。当然还有 document.domain+iframe、动态创建script、利用iframe和location.hash、window.name以及使用FLASH等等等。懒得试了。回到一开始就想到而立马被排除的方法:<form id='formid' method='post' action='页面提交URL' >...</form>,我把ACTION 提交的URL动态变更不就解决了嘛。哎,有时候碰到问题不要急于否认某个解决方案,还是应该冷静的下来把脑袋转个弯也许就是一个很好的解决方案。

    为了调用方便,我用CS写在后台:

 /// <summary>
    /// Summary description for HttpHelper
    /// </summary>
    /// <Author>jack che</Author>
    public static class HttpHelper
    {
        /// <summary>
        /// This method prepares an Html form which holds all data in hidden field in the addetion to form submitting script.
        /// </summary>
        /// <param name="url">The destination Url to which the post and redirection will occur, the Url can be in the same App or ouside the App.</param>
        /// <param name="data">A collection of data that will be posted to the destination Url.</param>
        /// <returns>Returns a string representation of the Posting form.</returns>
        /// <Author>jack che</Author>
        private static String PreparePostForm(string url, NameValueCollection data)
        {
            //Set a name for the form
            const string formId = "PostForm";

            //Build the form using the specified data to be posted.
            var strForm = new StringBuilder();
            strForm.Append("<form id=\"" + formId + "\" name=\"" + formId + "\" action=\"" + url + "\" method=\"POST\">");
            foreach (string key in data)
            {
                strForm.Append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + data[key] + "\">");
            }
            strForm.Append("</form>");

            //Build the JavaScript which will do the Posting operation.
            var strScript = new StringBuilder();
            strScript.Append("<script type='text/javascript'>");
            strScript.Append("var v" + formId + " = document." + formId + ";");
            strScript.Append("v" + formId + ".submit();");
            strScript.Append("</script>");

            //Return the form and the script concatenated. (The order is important, Form then JavaScript)
            return strForm + strScript.ToString();
        }
        /// <summary>
        /// POST data and Redirect to the specified url using the specified page.
        /// </summary>
        /// <param name="page">The page which will be the referrer page.</param>
        /// <param name="destinationUrl">The destination Url to which the post and redirection is occuring.</param>
        /// <param name="data">The data should be posted.</param>
        /// <Author>jack che</Author>
        public static void RedirectAndPost(Page page, string destinationUrl, NameValueCollection data)
        {
            //Prepare the Posting form
            string strForm = PreparePostForm(destinationUrl, data);

            //Add a literal control the specified page holding the Post Form, this is to submit the Posting form with the request.
            page.Controls.Add(new LiteralControl(strForm));
        }
    }
View Code

     调用方法:

using System.Collections.Specialized;

var data = new NameValueCollection {{"uid","test uid"}, {"upwd", "test pwd"}};
  HttpHelper.RedirectAndPost(Page, "http://testing.aspx", data);
View Code

 

无独有偶,看到一个台湾 博客 F6 Team 也写了一个类似的,只不过他把JS写在前台,各有利弊。附地址:http://www.dotblogs.com.tw/puma/archive/2008/09/03/5288.aspx