2011-04-14 87 views
3

在我的MVC 3 .net應用程序中,我有兩個區域,一個叫Admin,一個叫Student。我使用內置的會員系統進行用戶驗證,並在兩個區域之間進行統一。問題是,我想使用區域特定的登錄頁面,因爲這兩個區域在設計上有很大不同(Student是針對移動設備的)。據我所知,我只能指定一個登錄頁面Web.config中的應用,像這樣:區域特定的登錄頁面

<authentication mode="Forms"> 
    <forms loginUrl="~/Admin/Account/LogOn" timeout="2880" /> 
</authentication>` 

在這種情況下,我將如何實現對同一會員制度若干登錄頁面?

回答

6

只能有一個ASP.NET應用程序指定1個登錄網址,所以你需要做以下解決方法:

在每個ARAA有一個登錄控制器,以及在主登錄控制器應用程序的根。

在web.config中,請確保您有:

<configuration> 
    <location path="/Admin/Account/LogOn"> 
    <system.web> 
     <authorization> 
     <allow users="?"/> 
     </authorization> 
    </system.web> 
    </location> 
    <location path="/Student/Account/LogOn"> 
    <system.web> 
     <authorization> 
     <allow users="?"/> 
     </authorization> 
    </system.web> 
    </location> 
</configuration> 

在你的web.config配置窗體身份驗證的根應用程序中使用的登錄控制:

<forms loginUrl="~/LogOn" timeout="2880" /> 

然後在根登錄控制器在默認操作中執行以下操作:

// 
// GET: /LogOn 
public ActionResult Index(string returnUrl) 
{ 
    var area = returnUrl.TrimStart('/').Split('/').FirstOrDefault(); 

    if (!string.IsNullOrEmpty(area)) 
     return RedirectToAction("LogOn", "Account", new { area }); 

    // TODO: Handle what happens if no area was accessed. 
} 
4

您應該閱讀this白皮書。那裏描述了你的問題的解決方案。