ASP.NET付款表提交需要指导

时间:2010-10-13 13:58:02

标签: c# asp.net payment-gateway

伙计们,如果问题不那么有条理且不太清楚,我向我道歉。我赶时间:(

我的网络应用程序包含需要提交到另一个ASP.NET页面的付款表单(我们称之为http://vendor.com/getpay.aspx) 驻留在另一台服务器上。

该页面将执行一些mumbo-jumbo工作,然后将其重定向到真实状态 支付网关站点。

当我通过简单的HTML表单将我的付款表单发布到getpay.aspx时,它可以正常工作并重定向。

如果我将表单及其隐藏的输入更改为服务器端控件,则它不起作用。他们的页面正在抛出viewstate异常。

  1. 我需要将表单隐藏的输入作为服务器控件,以便我可以绑定由我的代码生成的一些值。(我想我可以像使用<%=%>的经典asp方式那样做,但它是比如回归标准!)
  2. 我在后面的代码中尝试了HttpWebRequest,它发布了表单,但浏览器没有重定向到Payment Gateway页面。
  3. 我在非https上发布付款信息,如何防止用户篡改发布的数据?
  4. 我想验证后端的付款方式然后发布,我无法信任用户输入数据。
  5. 此外,结果返回到我的重定向页面,并附加了查询字符串。它也发生在非https上。 我可以信任多少重定向数据?
  6. 很多

1 个答案:

答案 0 :(得分:3)

通过清除响应并将html HTTP表单重写为已清除的响应来生成表单。当我回到家时,我将浏览我的旧代码并提供一个示例。

编辑: 好的,这是我的代码,我不得不重新创建因为我还在工作,但它有点像这样:

创建一个中间页面以从ASPX页面捕获变量,然后使用它作为“简单”形式发送:

        protected void Page_Load(object sender, EventArgs e)
        {

            // Capture the post to this page
            IDictionary<string, string> variables = new Dictionary<string, string>();

            variables.Add("test", Request.Form["test"]); // collect all variables after checking they exist

            RewriteContent(variable);
        }

        public void RewriteContent(IDictionary<string, string> variables)
        {
            string formContent = @"
    <html>
        <head>
            <title>My Form</title>
        </head>
        <body>
            <form action='' method=''>";

            foreach (KeyValuePair<string, string> keyVal in variables)
            {
                formContent += @"<input type='" + keyVal.Key + "' value='" + keyVal.Value + "' />";
            }

        formContent += @"
            </form>
        </body>
    </html>"; // Add either an auto post in a javascript or an explicit submit button

            Response.Clear();
            Response.Write(formContent);
            Response.Flush();
            Response.End();
        }

编辑2: 对不起,我刚才意识到我没有回答其他问题。

Q3 / Q4 / Q5。如果你没有使用https,你不能真正停止篡改或确保响应是正确的,但你可以限制它是假的机会。这可以通过使用在您的目的地和目的地共享的秘密对值进行散列来实现,然后当您获得响应时,您应该散列值并与接受之前发送回给您的散列进行比较。有效。

大多数付款机制都以这种方式验证,通常使用MD5或SHA1哈希,您可以在以下链接中找到更多信息:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1.aspx http://www.developerfusion.com/code/4601/create-hashes-md5-sha1-sha256-sha384-sha512/ http://snippets.dzone.com/posts/show/5816

编辑3: 现在做一些加密,并认为我会与你分享一些代码(因为我很喜欢)。可能会让你知道该怎么做,你可能比我更好地编码,所以只是稍微整理我的烂摊子:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using log4net;

namespace MyCompany.Cipher
{
    private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    public string GenerateSha1HashForString(string valueToHash, EncodeStyle encodeStyle)
    {
        string hashedString = string.Empty;

        try
        {
            hashedString = SHA1HashEncode(Encoding.UTF8.GetBytes(valueToHash), encodeStyle);
        }
        catch (Exception ex)
        {
            if (log.IsErrorEnabled) { log.Error(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace)); }
            throw new Exception("Error trying to hash a string; information can be found in the error log", ex);
        }

        return hashedString;
    }

    private string ByteArrayToString(byte[] bytes, EncodeStyle encodeStyle)
    {
        StringBuilder output = new StringBuilder(bytes.Length);

        if (EncodeStyle.Base64 == encodeStyle)
        {
            return Convert.ToBase64String(bytes);
        }

        for (int i = 0; i < bytes.Length; i++)
        {
            switch (encodeStyle)
            {
                case EncodeStyle.Dig:
                    //encode to decimal with 3 digits so 7 will be 007 (as range of 8 bit is 127 to -128)
                    output.Append(bytes[i].ToString("D3"));
                    break;
                case EncodeStyle.Hex:
                    output.Append(bytes[i].ToString("X2"));
                    break;
            }
        }

        return output.ToString();
    }

    private string SHA1HashEncode(byte[] valueToHash, EncodeStyle encode)
    {
        SHA1 a = new SHA1CryptoServiceProvider();
        byte[] arr = new byte[60];
        string hash = string.Empty;

        arr = a.ComputeHash(valueToHash);
        hash = ByteArrayToString(arr, encode);

        return hash;
    }
}

将它放在项目可以看到的某个类中,并且可以通过调用public方法生成基于字符串值的SHA1哈希值。

相关问题