最近公司領導要求,IIS網站要由經典模式改為集成模式,以提高性能。改完之后,登錄成功跳轉到主頁之后,頁面提示“”HTTP 錯誤 401.0 - Unauthorized“,“您無權查看此目錄或頁面”,截圖如下:
網上找了很多資料,都沒法解決。先看看我的網站IIS配置目錄:
IIS配置為網站,其中網站根目錄下,還有個子應用程序,我們假設網站應用程序名為WebSite,子應用程序名為Portal,其中WebSite網站對應的文件夾下有Web.config,Portal子應用程序對應的文件夾下也有web.config。而WebSite文件夾下的web.config為根目錄配置文件,且配置了form認證,配置如下:
<authentication mode="Forms"> <forms cookieless="UseCookies" loginUrl="/Portal/Account/Login" protection="None" timeout="120"></forms> </authentication>
按里說,這里配置了form認證,登錄成功就可以跳轉到主頁,但事實上並非如此,一直提示上面的報錯。后來看了網上的一片帖子,找到了答案:
https://stackoverflow.com/questions/19536955/request-isauthenticated-is-always-false
其中Ger Groot給出答案中,解決了我的問題:
<modules> <remove name="FormsAuthentication" /> <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" /> </modules>
原來只需要在我的子應用程序web.config文件中,system.webServer目錄下,添加以上代碼就行了,問題解決。
后來再仔細思索了一下,發現
<remove name="FormsAuthentication" />
這段原本就在子應用程序web.config配置中存在,莫非就是因為這個導致子應用程序portal沒有權限?索性去掉這句,也無需添加
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
問題得到解決。