使用 asp.net mvc 实现输出编码以防止应用程序级别的 Xss 攻击 [英] Implement output encoding to prevent Xss attack at application level using asp.net mvc

查看:36
本文介绍了使用 asp.net mvc 实现输出编码以防止应用程序级别的 Xss 攻击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有任何代码可以在这里发布.我只想在应用程序级别应用输出编码.以便对每个操作方法的输出进行编码并显示在 UI 上.我正在寻找一种解决方案,可以通过覆盖 asp.net mvc 中的过滤器来实现这一点.提供的任何示例代码都会有所帮助.提前致谢

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

推荐答案

如果您使用的是 razor 页面,那么您无需担心 html 编码,为了防止 xss,您可以使用 Antiforiegn() .... 这里是一种从检索到的数据中过滤html标签的方法.

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.

使用此函数删除 HTML 标签

Use this function to remove HTML tags

说明.

1)RemoveHTMLTags(string str) 获取json字符串.

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

2) 将 json 字符串与正则表达式<[^>]*>"进行比较.

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

3) 如果发现任何 HTMl 标签,从 json 字符串中删除该标签.

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

4) 返回不含 html 标签的 Json 字符串.

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;
}

这篇关于使用 asp.net mvc 实现输出编码以防止应用程序级别的 Xss 攻击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆