Implement output encoding to prevent Xss attack at application level using asp.net mvc

245 Views Asked by At

I have dont have any code to post here. I just want to apply output encoding at application level . so that output of each action method is encoded and displayed on UI . I am looking for a solution where this can be achieved by overriding the filters in asp.net mvc. Any example code provided would be helpful. Thanks in advance

1

There are 1 best solutions below

0
Syed Akhter On

if you are using razor pages then you don't need to worry about html encoding and in order to prevent xss you can Antiforiegn() .... Here is a method to filter the html tags from the retrieved data.

Use this function to remove HTML tags

Description.

1)RemoveHTMLTags(string str) gets the json string.

2)compare the json string with regex "<[^>]*>".

3)if found any HTMl tag remove the tag from the json string.

4)Return Json string free from html tags.

public  string RemoveHTMLTags(string str) 
 { 
 System.Text.RegularExpressions.Regex rx =
 new System.Text.RegularExpressions.Regex("<[^>]*>");
 str = rx.Replace(str, "");            
 return str;
}