|
|
|
|
|
web.config 設(shè)置 X-Frame-Options
的方法,不少文章都有介紹過,代碼看似簡單,但一不小心就會(huì)出現(xiàn)錯(cuò)誤,例如代碼塊放錯(cuò)了地方。這里我提供一個(gè)完整的web.config代碼,可以更清楚的看到代碼塊的位置。
代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Frame-Options" value="SAMEORIGIN" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
說明, <httpProtocol>...</httpProtocol>
整塊代碼一定要放到 <system.webServer>...</system.webServer>
里面,尤其要注意的是, <system.webServer>...</system.webServer>
與 <system.web>...</system.web>
是并列的代碼塊,相互不能被包含。
X-Frame-Options 有三個(gè)值:
表示該頁面不允許在 frame 中展示,即便是在相同域名的頁面中嵌套也不允許。
表示該頁面可以在相同域名頁面的 frame 中展示。
表示該頁面可以在指定來源的 frame 中展示。uri
是網(wǎng)頁地址,如:http://www.howtostagehomes.com/
做項(xiàng)目時(shí),有可能遇到 web.config 設(shè)置 X-Frame-Options 無效的情況,請確認(rèn)自己已經(jīng)重啟了IIS。如果不行,試試下面的代碼:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy" value="default-src: https:; frame-ancestors 'self' X-Frame-Options: SAMEORIGIN" />
</customHeaders>
</httpProtocol>
</system.webServer>
代碼解釋,您的 web.config 條目需要在 Content-Security-Policy
(內(nèi)容安全策略)下才能使用之前沒有折舊的編碼。 value ="default-src: https:"
內(nèi)容安全策略下的值對您的網(wǎng)站是唯一的。
重要的內(nèi)容是 'value =“default-src: https:'
后面的內(nèi)容,但最重要的是包含在內(nèi)容安全策略中。