2014-01-21 232 views
1

我設置一個基本的MVC 4應用到每個一些老instructions使用AntiXssEncoder在web.config。當我這樣做時,雖然通常會被編碼的應用程序字符不再被正確編碼。AntiXssEncoder在web.config中弄亂了默認的編碼方案

反正在web.config糾正這種不可接受的行爲,或者這個庫剛剛打破?

<httpRuntime targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 

MVC4默認的web.config: 此展品預期的行爲

<table> 
    <tr> 
     <td>No encoding</td> 
     <td>Inside Microsoft&#174; SharePoint&#174; 2013</td> 
    </tr> 
    <tr> 
     <td>HttpUtility.HtmlEncode("Inside Microsoft® SharePoint® 2013")</td> 
     <td>Inside Microsoft&amp;#174; SharePoint&amp;#174; 2013</td> <!-- double encoded, expected result --> 
    </tr> 
    <tr> 
     <td>AntiXssEncoder.HtmlEncode("Inside Microsoft® SharePoint® 2013")</td> 
     <td>Inside Microsoft&#174; SharePoint&#174; 2013</td><!-- manual call to encode, expected result --> 
    </tr> 
</table> 

AntiXssEncoder設置在web.config中: 這是預期的行爲。沒有任何版權符號已被正確編碼。

<table> 
    <tr> 
     <td>No encoding</td> 
     <td>Inside Microsoft® SharePoint® 2013</td> 
    </tr> 
    <tr> 
     <td>HttpUtility.HtmlEncode("Inside Microsoft® SharePoint® 2013")</td> 
     <td>Inside Microsoft® SharePoint® 2013</td> 
    </tr> 
    <tr> 
     <td>AntiXssEncoder.HtmlEncode("Inside Microsoft® SharePoint® 2013")</td> 
     <td>Inside Microsoft® SharePoint® 2013</td> 
    </tr> 
</table> 

的源代碼參考:

<table> 
    <tr> 
     <td>No encoding</td> 
     <td>@data</td> 
    </tr> 
    <tr> 
     <td>HttpUtility.HtmlEncode("Inside Microsoft® SharePoint® 2013")</td> 
     <td>@HttpUtility.HtmlEncode(data)</td> 
    </tr> 
    <tr> 
     <td>AntiXssEncoder.HtmlEncode("Inside Microsoft® SharePoint® 2013")</td> 
     <td>@System.Web.Security.AntiXss.AntiXssEncoder.HtmlEncode(data, false)</td> 
    </tr> 
</table> 

回答

0

我沒能找到老資源,其中AntiXssEncoder.HtmlEncode()只能有一個字符串作爲參數。然而看着MSDN - AntiXssEncoder.HtmlEncode(string,boolean),你得到的結果是預期的。

註冊商標字符是安全字符(C1 Controls and Latin-1 Supplement)的一部分,因此它不會被編碼。

如果調用傳遞真實的useNamedEntities參數的方法,註冊商標將被轉換爲& REG;

你可以參考下面的代碼 - 也.Net Fiddle

using System; 
using System.Web; 
using System.Web.Security.AntiXss; 

public class Program 
{ 
    public static void Main() 
    { 
     string data = "Inside Microsoft® SharePoint® 2013"; 

     Console.WriteLine(" -- HttpUtility.HtmlEncode(data) --"); 
     Console.WriteLine(HttpUtility.HtmlEncode(data)); 

     Console.WriteLine(" -- AntiXssEncoder.HtmlEncode(data, false) --"); 
     Console.WriteLine(AntiXssEncoder.HtmlEncode(data, false)); 

     Console.WriteLine(" -- AntiXssEncoder.HtmlEncode(data, true) --"); 
     Console.WriteLine(AntiXssEncoder.HtmlEncode(data, true)); 
    } 
}