简体   繁体   English

如何在 Asp.Net Core 中全局防止 XSS 和 CSRF

[英]How to prevent XSS and CSRF globally in Asp.Net Core

I want to prevent my application from XSS and CSRF attacks, I want to make some global check to prevent these.我想防止我的应用程序受到XSSCSRF攻击,我想进行一些全局检查以防止这些。

I have used below code in statup file in ConfigureServices function, it prevents CSRF attacks but I am not sure is it enough to prevent both the attacks or do I need to write some code to prevent XSS separately.我在ConfigureServices函数的statup文件中使用了以下代码,它可以防止 CSRF 攻击,但我不确定是否足以防止这两种攻击,或者我是否需要编写一些代码来单独防止 XSS。

services.AddMvc(options => 
    options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()));

One more doubt : will it work for an API application?还有一个疑问:它适用于 API 应用程序吗?

XSS and CSRF are completely different kinds of attacks which have to be defended against in completely different ways. XSS 和 CSRF 是完全不同类型的攻击,必须以完全不同的方式进行防御。

XSS requires that user input be appropriately escaped for the very specific context in which it is inserted. XSS 要求用户输入在其插入的特定上下文中进行适当的转义。 There is no panacea for it.没有灵丹妙药。 You cannot solve it globally.你不能全局解决它。 (There are various techniques which look for suspicious content in requests which might be XSS but they are rather prone to throwing false positives). (有多种技术可以在可能是 XSS 的请求中查找可疑内容,但它们很容易引发误报)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM