2015-09-08 20 views
2

我有一個面向ASP.NET網站的Internet,我想通過Windows身份驗證來保護它。我把我的web.config文件:使用ASP.NET的IIS中的自定義401頁面

<system.web> 
<authentication mode="Windows" /> 
<authorization> 
    <allow users="*"/> 
    <deny users="?"/>   
</authorization> 

然後我已禁用匿名訪問和IIS 7.5中啓用Windows身份驗證。

這會導致爲我的Windows憑據顯示提示框,但單擊「取消」會給我一個標準的401錯誤頁面。我想顯示的地方此消息的靜態HTML文件,但是我已經無法得到它的工作,我已經嘗試過的各種設置,如組合:

<httpErrors errorMode="Custom" existingResponse="Replace" lockAllAttributesExcept="errorMode"> <error statusCode="401" prefixLanguageFilePath="c:\inetpub\custerr" path="MyCustom401.htm" /> </httpErrors> 

<customErrors mode="Off" defaultRedirect="ErrorPage.aspx"> 
    <error statusCode="401" redirect="MyCustom401.aspx" /> 
</customErrors> 

我想要發生的是,任何輸入正確的Windows憑據的人都可以像往常一樣進入網站,但那些無效或詳細信息的人可以看到自定義HTML頁面。

任何人都可以指向正確的方向嗎?

謝謝!

+0

Web瀏覽器通常顯示出他們的錯誤頁面的版本,所以你真的想改變IIS的? –

+0

我想向用戶顯示一個自定義頁面,該頁面將提供有關如何獲取有效憑據以便能夠登錄到網站的說明。所以是的,我想能夠覆蓋他們看到的錯誤頁面。 – ca8msm

+1

這是根本不可能的,因爲網頁瀏覽器端設置具有最高優先級。您將不得不重定向到特定頁面並返回200響應代碼而不是401。 –

回答

1

有一點要確定的是,您允許匿名用戶訪問包含錯誤文件的路徑,否則他們將不會收到錯誤頁面。例如,這裏是一個配置文件,如果您的錯誤文件位於一個目錄(錯誤)中,該配置文件應該能夠爲您提供預期的結果。首先,它禁用所有網站的匿名訪問,但隨後打開它的「錯誤」的文件夾:

<configuration> 
    <system.webServer> 
     <security> 
      <authorization> 
       <add accessType="Deny" users="?" /> 
      </authorization> 
     </security> 
     <httpErrors errorMode="Custom"> 
      <error statusCode="401" subStatusCode="2" path="/errors/unauthorized.aspx" responseMode="ExecuteURL" /> 
     </httpErrors> 
    </system.webServer> 

    <location path="errors"> 
     <system.webServer> 
      <security> 
       <authorization> 
        <clear /> 
        <add accessType="Allow" users="*" /> 
       </authorization> 
      </security> 
     </system.webServer> 
    </location> 
</configuration>