代码之家  ›  专栏  ›  技术社区  ›  Simon Johnson Andomar

以编程方式关闭请求验证

  •  2
  • Simon Johnson Andomar  · 技术社区  · 17 年前

    该控件允许网站所有者调整该页面上的内容。如果愿意,他们可能会输入标记。因为这是他们的网站编辑,他们必须能够坚持任何他们想在那里。

    我想知道是否有可能以编程方式禁用此验证?

    3 回复  |  直到 17 年前
        1
  •  3
  •   Stumblor    12 年前

    HttpRuntime.RequestValidationMode 在此之前的财产 Pages.ValidateRequest 财产将得到认可。

    public void ModifiyValidation(bool validate) {
    
        var pagesSection = System.Configuration.ConfigurationManager.GetSection("system.web/pages") as PagesSection;
        var httpRuntime = System.Configuration.ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection;
    
        if (pagesSection != null && httpRuntime != null && pagesSection.ValidateRequest != validate)
        {
            var fi = typeof (ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            fi.SetValue(pagesSection, false);
            fi.SetValue(httpRuntime, false);
            pagesSection.ValidateRequest = validate;
            httpRuntime.RequestValidationMode = new Version(validate ? "4.0" : "2.0");
            fi.SetValue(pagesSection, true);
            fi.SetValue(httpRuntime, true);
        }
    }
    

    您还应注意,将仅在上激活 下列的 要求

        2
  •  2
  •   cgreeno    17 年前

    PagesSection pageSection = new PagesSection();
    pageSection.ValidateRequest = false;
    

    Reference

        3
  •  1
  •   Simon Johnson Andomar    17 年前

    我所做的是关闭web.config中的设置,并使用HTTP模块对用户未处于编辑模式的所有请求进行请求验证。

    推荐文章