2012-11-18 26 views

Trả lời

4

Không có cách tự động. Việc gần nhất bạn có thể làm là để có được gói AntiXss Nuget. Sau đó, bạn có thể sử dụng nó như dưới đây trong điều khiển của bạn:

Microsoft.Security.Application.Sanitizer.GetSafeHtml("YourHtml"); 

HOẶC

Microsoft.Security.Application.Encoder.HtmlEncode("YourHtml"); 

Nếu bạn sử dụng, bạn có thể giải mã nó bằng cách sử

Server.HtmlDecode("HtmlEncodedString"); 

Hope this helps.

10

Trước tiên, không có gì được tích hợp sẵn cho điều đó. Nhưng MVC cho phép để làm những việc như vậy một cách dễ dàng thông qua ModelBinders tùy chỉnh, bạn có thể xác định bạn

public class CustomAntiXssAttribute : Attribute { } 

và trang trí tài sản của bạn với nó (và thậm chí kế thừa từ AllowHtmlAttribute nếu bạn muốn). Sau đó, với một chất kết dính mô hình mà bạn có thể thêm bảo vệ chống XSS cụ thể của bạn:

public class CutstomModelBinder : DefaultModelBinder 
    { 
     protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) 
     { 
      if (propertyDescriptor.Attributes.OfType<CustomAntiXssAttribute>().Any()) 
      { 
       var valueResult = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name); 
       var filteredValue = SOME_CUSTOM_FILTER_FUNCTION_HERE(valueResult.AttemptedValue); 
       propertyDescriptor.SetValue(bindingContext.Model, filteredValue); 
      } 
      else // revert to the default behavior. 
      { 
       base.BindProperty(controllerContext, bindingContext, propertyDescriptor); 
      } 
     } 
    } 

Sau đó bên SOME_CUSTOM_FILTER_FUNCTION_HERE mà bạn có thể sử dụng những gì @Yogiraj đề nghị, hoặc sử dụng một biểu thức chính quy, hoặc thậm chí áp dụng lọc HtmlAgilityPack-based.

P.S. Đừng quên để thêm ModelBinders.Binders.DefaultBinder = new CutstomModelBinder(); để Application_Start (Tôi quên :))

+3

BẠN KHÔNG THỂ kế thừa từ ALLOWHTMLATTRIBUTE "Đó là một lớp niêm phong" – VJAI

0

Mã chưa được kiểm tra,

public class ADefaultModelBinder : DefaultModelBinder 
{ 
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     if (bindingContext.ModelMetadata.RequestValidationEnabled) 
     { 
      var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue; 
      value = value.Replace("&", "");// replace existing & from the value 
      var encodedValue = Microsoft.Security.Application.Encoder.HtmlEncode(value); 
      bindingContext.ModelMetadata.RequestValidationEnabled = encodedValue.Contains("&"); // Whether AntiXss encoded a char to &.. 
     } 
     return base.BindModel(controllerContext, bindingContext); 
    } 
} 
public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     ModelBinders.Binders.DefaultBinder = new ADefaultModelBinder(); 
1

tôi sẽ đi để thay thế những AllowHtml thuộc tính với RegularExpression kiểm chứng thực dữ liệu chú thích. Ưu điểm là bằng cách này, bạn có thể bẫy lỗi và hiển thị cho người dùng những gì đã xảy ra trong khi lỗi trước đây kích hoạt lỗi ở mức toàn cầu.

Ví dụ:

public class MyViewModel 
{ 
    [DataType(DataType.MultilineText)] 
    [RegularExpression(@"^[^\<\>]*$", ErrorMessage = "May not contain <,>")] 
    public string Text { get; set; } 
} 

Ref: RegularExpression validator encoding regex < & > symbols as &lt; &gt;, causing jQuery validation to fail

Các vấn đề liên quan