2015-10-21 26 views
0

篡改所以我有一個MVC5 Web應用程序,我想用支付網關整合,我必須做到以下幾點HTTP POST到支付網關的URL。驗證HTTP POST並確保數據不被用戶

@Html.BeginForm(null, null, FormMethod.Post, new { @action = "https://l33tpaymentgateway.com" }) 
{ 
    <input id="MerchantCode" name="MerchantCode" type="hidden" value="12345" /> 
    <input id="RefNo" name="RefNo" type="hidden" value="ABCDE" /> 
    <input id="Amount" name="Amount" type="hidden" value="300" /> 
    <input id="Currency" name="Currency" type="hidden" value="USD" /> 
    <input id="UserEmail" name="UserEmail" type="hidden" value="[email protected]" /> 
    <input id="Signature" name="Signature" type="hidden" value="1234567890" /> 
    <input id="ResponseURL" name="ResponseURL" type="hidden" value="http://warheat1990.com" /> 

    <input type="submit" value="submit"/> 
} 

正如你可以看到數據可以容易地通過用戶編輯(例如檢查用鉻元素和他們可以只改變隱藏輸入的值),這意味着我需要做一堆的驗證,是有可能在我的服務器端做一個HTTP POST,而不是在之後重定向用戶?或者有沒有其他方法可以防止用戶篡改HTML值?

回答

0

您可以在服務器上做的一切:

public async Task<ActionResult> SubmitPayment() 
{ 
     using (var client = new HttpClient()) 
     { 
      client.BaseAddress = new Uri("https://l33tpaymentgateway.com"); 
      var content = new FormUrlEncodedContent(new[] 
      { 
       new KeyValuePair<string, string>("MerchantCode", "12345"), 
       new KeyValuePair<string, string>("RefNo", "ABCDE"), 
       //add other properties here 
      }); 
      var result = await client.PostAsync("", content); 
      if(result.IsSuccessStatusCode) 
      { 
       //Payment successfull 
       if you need to read response content: 
       var responseContent = await result.Content.ReadAsStringAsync();     
      } 
      else 
      { 
       you got error 
      } 

     } 
} 

這個動作可以從客戶端或者通過AJAX或

@Html.BeginForm("SubmitPayment", "ControllerName", FormMethod.Post) 
{ 
    <input type="submit" value="submit"/> 
} 
+0

一個問題,我怎麼重定向用戶到支付網關頁面,如果'IsSuccessStatusCode == TRUE'?因爲'responseContent'只是一個HTML字符串。 – warheat1990

+0

如果付款成功,您需要重定向用戶嗎?如果是的話只返回'重定向(「重定向URL」);' –

+0

沒有我需要重定向到任何鏈接'l33tpaymentgateway'成功POST之後提供。我爲此打開了另一個線索,但迄今爲止沒有運氣。 http://stackoverflow.com/questions/33257254/how-to-perform-http-post-and-redirect-to-external-site-from-the-post-result – warheat1990

0

你不應該從用戶的瀏覽器直接提交請求支付網關被觸發。請求支付網關向用戶透露太多信息。通過查看請求數據,用戶可以操縱任何事物。

相反,你應該安全地存儲在您的服務器上的所有支付網關相關的信息和用戶應當提交請求到服務器,讓你的服務器發佈請求支付網關。除了提升安全性這種方法,你會鼓勵你起碼的信息發送到您的服務器,因爲你可能已經擁有用戶的詳細信息(如電子郵件地址)在您的系統,並會提供各地的支付網關抽象。