A potentially dangerous Request.Form value was detected from the client

前端 未结 30 2136
刺人心
刺人心 2020-11-21 05:24

Every time a user posts something containing < or > in a page in my web application, I get this exception thrown.

I don\'t want to go

30条回答
  •  我在风中等你
    2020-11-21 05:34

    Another solution is:

    protected void Application_Start()
    {
        ...
        RequestValidator.Current = new MyRequestValidator();
    }
    
    public class MyRequestValidator: RequestValidator
    {
        protected override bool IsValidRequestString(HttpContext context, string value, RequestValidationSource requestValidationSource, string collectionKey, out int validationFailureIndex)
        {
            bool result = base.IsValidRequestString(context, value, requestValidationSource, collectionKey, out validationFailureIndex);
    
            if (!result)
            {
                // Write your validation here
                if (requestValidationSource == RequestValidationSource.Form ||
                    requestValidationSource == RequestValidationSource.QueryString)
    
                    return true; // Suppress error message
            }
            return result;
        }
    }
    

提交回复
热议问题