2015-11-08 66 views
1

假設我希望使用antiXSS將我的項目的用戶控件中的<input>標記編碼爲XSS。無法從'System.Web.UI.HtmlControls.HtmlInputText'轉換爲'string'與HtmlEncode AntiXSS

searchbox.ascx

<input id="searchBox" type="text" class="QueryBox" runat="server" /> 

searchbox.ascx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Microsoft.Security.Application; 

namespace Project2015.Website.layout.search 
{ 
    public partial class searchbox : System.Web.UI.UserControl 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      searchBox = Encoder.HtmlEncode(searchBox); 
     } 


    } 
} 

當我嘗試編譯我的代碼,我收到此錯誤

Error 108 Argument 1: cannot convert from 'System.Web.UI.HtmlControls.HtmlInputText' to 'string'

Error 107 The best overloaded method match for 'Microsoft.Security.Application.Encoder.HtmlEncode(string)' has some invalid arguments

不能看到什麼我在這裏做錯了。

回答

0

搜索框是HTML控件。在編碼中使用文本屬性。

searchBox.Text = Encoder.HtmlEncode(searchBox.Text);

或者你可以直接在aspx文件中使用encode。

我建議最好在代碼中創建用於編碼和使用幫助函數的幫助函數。如果您想應用某些條件,稍後可以更輕鬆地進行自定義。

+0

我收到以下錯誤:System.Web.UI.HtmlControls.HtmlInputText'不包含「文本」的定義,也沒有接受類型爲「System.Web.UI」的第一個參數的擴展方法「文本」。 HtmlControls.HtmlInputText'可以找到(你是否缺少使用指令或程序集引用? – Phosy

+0

嘗試.Value而不是.Text –

+0

與.Value我得到一個NullReferenceException [NullReferenceException:對象引用未設置爲對象的實例。] C:\ inetpub \ wwwroot \ Project2015 \ Website \ layout \ search \ searchbox.ascx.cs中的Project2015.Website.layout.searchbox.Page_Load(Object sender,EventArgs e):15 System.Web.UI.Control .OnLoad(EventArgs e)+108 System.Web.UI.Control.LoadRecursive()+67 System.Web.UI.Control.LoadRecursive()+164 System.Web.UI.Control.LoadRecursive()+164 System.Web.UI.Page.ProcessRequestMain(布爾includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint)4497米 – Phosy

相關問題