1. 程式人生 > >forms身份認證保護html頁面

forms身份認證保護html頁面

asp.net的forms身份認證保護是一個非常棒的東西,用VS2010建立一個Web應用程式即可看到範例

在web.config中配置

          <authentication mode="Forms" >

            <forms loginUrl="~/Account/Login.aspx" protection="All" timeout="2880" />

        </authentication>

這樣就可以指定驗證失敗後跳轉到頁面,然後再你想驗證的Account檔案下面再放一個Web.config

<configuration>


  <location path="Register.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>


  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>


</configuration>

這樣就表示除了account檔案下的Register.aspx可以不用通過身份驗證外,其他的都必須通過身份驗證。

但是這裡有個問題。我在account下放了個1.html的檔案。在VS中瀏覽這個檔案時,會被強行跳轉到登入頁。但是我把這個專案釋出之後,在IIS 卻可以直接瀏覽這個html檔案。

在身份驗證的配置中,protection="All",會保護所有的aspx檔案,但是html並不會保護。所以,要想IIS中html檔案得到forms的身份驗證必須配置html檔案以aspx檔案的方式工作

配置分兩步:

1.在網站的web.config下增加: <httpHandlers>  <add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"  validate="true" />    </httpHandlers>放在<system.web>下。同時在<compilation debug="true" targetFramework="4.0">下新增 <buildProviders> <add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
          </buildProviders>

2。進入iis中的網站,屬性--->主目錄--->配置:在應用程式那裡可以看到已經配置好的.aspx檔案。點選新增:"可執行檔案",建立一個.html,填寫方式和.aspx一樣,自己負責貼上。

配置好之後,.html檔案就會以.aspx檔案的方式工作。

再次瀏覽網站時,account檔案下的1.html同樣需要身份驗證才能訪問了。