2012-01-31 37 views
4

我們使用HttpModule掛接到FormsAuthenticationModule並訂閱Authenticate事件。當我們使用Web窗體時,這個事件在模塊中觸發。當我們使用MVC時,這個事件不會觸發。FormsAuthenticationModule在使用ASP.NET MVC時不會觸發身份驗證事件

我已經嘗試使用控制器上的[Authorize]屬性和web.config中的位置(儘管這不是最佳實踐)嘗試讓該事件觸發但它仍然沒有。

事件在使用Cassini網絡服務器時觸發,但不在IIS 7.5或IIS Express上觸發。我們正在運行ASP.NET MVC 2使用.net 3.5

編輯

身份驗證事件觸發時,我們要求的.aspx或.ashx的文件。如果我們請求擴展名文件或.css或.js,它也不會觸發。

新的ASP.NET MVC應用程序將爲請求的每個文件觸發此事件。

有什麼建議嗎?

+0

表單身份驗證是否在MVC中正確工作? – 2012-01-31 19:51:43

+0

@MaxToro是的。如果我已經導航到一個aspx頁面,自定義HttpHandlder將已經運行並設置表單cookie。 – 2012-01-31 20:00:22

回答

2

我們的web.config缺少來自system.webServer中modules元素的runAllManagedModulesForAllRequests =「true」。一旦添加了所有的Web請求,就會從FormsAuthenticationModule接收授權事件。

<system.webServer> 
    .... 
    <modules runAllManagedModulesForAllRequests="true"> 
    .... 
</system.webServer> 
+0

接受你的答案。 – 2012-02-01 03:22:14

+0

@AmarPalsapure我會但是不允許你這樣做一天。 – 2012-02-01 05:28:43

+0

嗯我不知道,至今沒問過問題。 – 2012-02-01 05:33:50

0

我不認爲這是解決這個問題的最好辦法,但我還使用MVC和formpages的組合和設置所有的授權在web.config

<location path="[path]"> 
    <system.web> 
    <authorization> 
     <allow users="[username]" roles="[role]"/> 
     <deny users="*"/> 
     </authorization> 
    </system.web> 
</location> 
+0

我試過這個來看看它是否有幫助,但它沒有。這樣爲MVC頁面配置安全性也是一種安全風險。 – 2012-01-31 20:00:50

+0

只是爲了個人信息,使用這種方法會形成哪種安全風險?不知道它怎麼不適合你。 – DerDee 2012-02-01 10:12:07

2

導航到aspx頁面不會測試表單身份驗證是否在MVC中工作,您必須導航到路徑。我看到了你的答案,這就是我的想法。我建議刪除managedHandler前提條件:

 <remove name="FormsAuthentication"/> 
    <add name="FormsAuthentication" preCondition="" type="System.Web.Security.FormsAuthenticationModule"/> 

    <remove name="DefaultAuthentication"/> 
    <add name="DefaultAuthentication" preCondition="" type="System.Web.Security.DefaultAuthenticationModule"/> 

    <remove name="RoleManager"/> 
    <add name="RoleManager" preCondition="" type="System.Web.Security.RoleManagerModule"/> 

    <remove name="UrlAuthorization"/> 
    <add name="UrlAuthorization" preCondition="" type="System.Web.Security.UrlAuthorizationModule"/> 

    <remove name="UrlRoutingModule-4.0"/> 
    <add name="UrlRoutingModule-4.0" preCondition="runtimeVersionv4.0" type="System.Web.Routing.UrlRoutingModule"/> 
+0

感謝您的回答。許多人建議關閉runAllManagedModulesForAllRequests,但不要列出大多數MVC網站工作所需的這些模塊 – 2012-10-02 15:42:44