我在IIS上运行ASP.NET MVC 3网站.web.config中是否有一个标志或类似的东西可以做到这一点?
Ste*_*n V 49
只要您使用的是IIS 7或更高版本,就像将其添加到web.config一样简单.
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
或者您可以使用IIS管理GUI甚至命令行添加它们.请查看http://www.iis.net/configreference/system.webserver/httpprotocol/customheaders
这个问题源自 MVC 3,但由于这个问题在 ASP.NET Core 中仍然相关,我将让自己为最新版本提出一个解决方案:
public static IApplicationBuilder UseNoSniffHeaders(this IApplicationBuilder builder)
{
return builder.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Content-Type-Options", "nosniff");
await next();
});
}
Run Code Online (Sandbox Code Playgroud)
然后只需将其添加到 Startup.cs 中:
app.UseNoSniffHeaders();
Run Code Online (Sandbox Code Playgroud)
这种方法的优点在于它使其独立于您的 Web 服务器和部署过程。同时,如果您希望它也适用于静态文件,您可能需要扩展此解决方案。
| 归档时间: |
|
| 查看次数: |
28379 次 |
| 最近记录: |