网站检测提示 X-Content-Type-Options Header Missing,在网上找资料发现都是其他的设置方法,没有
在网上找解决办法,都是说添加X-CONTENT-TYPE-OPTIONS to NOSNIFF
,而网上资料都是php的解决方法,C#的解决方法很少。
在web.config 配置文件中添加如下响应头
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff"/>
<add name="X-XSS-Protection" value="1;mode=block"/>
<add name="X-Frame-Options" value="SAMEORIGIN"/>
<add name="Content-Security-Policy" value="default-src 'self' i.tianqi.com"/>
</customHeaders>
</httpProtocol>
</system.webServer>
We started facing this error in production after our devops team changed the webserver configuration by adding X-Content-Type-Options: nosniff. Now, due to this, browser was forced to interpret the resources as it was mentioned in content-type parameter of response headers.
Now, from the beginning, our application server was explicitly setting content-type of the js files as text/plain. Since, X-Content-Type-Options: nosniff was not set in webserver, browser was automatically interpreting the js files as JavaScript files although the content-type was mentioned as text/plain. This is called as MIME-sniffing. Now, after setting X-Content-Type-Options: nosniff, browser was forced to not do the MIME-sniffing and take the content-type as mentioned in response headers. Due to this, it did interpret js files as plain text files and denied to execute them or blocked them. The same is shown in your errors.
Solution: is to make your server set the content-type of JS files as
application/javascript;charset=utf-8
This way, it will load all JS files normally and issue will get resolved.
在《Hardening your HTTP response headers》文章中,作者讲的更清楚。
参考:https://zp.xz-hc.com/news/show-5207.html
https://stackoverflow.com/questions/40728554/resource-blocked-due-to-mime-type-mismatch-x-content-type-options-nosniff
Hardening your HTTP response headers,https://scotthelme.co.uk/hardening-your-http-response-headers/#x-content-type-options
https://www.ilovefreesoftware.com/09/featured/check-if-x-xss-protection-is-enabled-in-http-header-on-your-website.html