2015-10-16 37 views
0

我正在開發一個項目,其中我有一個後臺區域(管理區域),配置系統和一個前臺區域,供典型用戶開展業務。ASP MVC5身份2.0後端和前端登錄

即是說,應用程序需要有一個後臺登錄和另一個Frontoffice登錄。每個人都必須分開。

我已經開發使用,ASP身份2.0,Backoffice和Frontoffice登錄和註冊不同領域,運作良好。

例如,可以爲後臺區域設置「myuser」用戶名和前臺區域的「myuser」用戶名,因爲每個用戶都保存在不同的表中。

我開發了一個自定義區域授權來管理這個。

我的問題是,當用戶例如到後臺辦公室面積登錄,如果他去frontoffice區,他仍然報已被記錄在...

我怎麼能分開User.Identity登錄每個區域?

在此先感謝!

+0

我建議使用另一種方法。我認爲你正在混合兩個不同的概念:認證和授權。用戶通過身份驗證後,您應該檢查(授權)用戶是否有權訪問管理區域(可以檢查用戶是否具有「admin」角色)。 –

+0

我也想到了這一點,但客戶端不想混用CMS和應用程序登錄...我準備的東西的方式,應用程序可以使用兩個不同的登錄名,但如果您登錄到CMS,User.Identity。 IsAuthenticated會說,用戶登錄的任何區域,我無法找到一種方法來檢查登錄是否在CMS或Frontoffice上完成...用戶角色是可能的,但我很想看看這是否可以工作與否! :)也嘗試使用不同的餅乾,但沒有這樣的運氣...... –

+0

順便說一下,我認爲有可能管理多個身份,因爲System.Security.Claims.ClaimsPrincipal有一個身份列表。 –

回答

0

解決方案通過:

1)創建前廳授權屬性和BO授權屬性管理FO/BO登錄網頁:

public class AreaAuthorizeAttribute : AuthorizeAttribute 
{ 
    private readonly string area; 

    public AreaAuthorizeAttribute(string area) 
    { 
     this.area = area; 
    } 

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     RouteValueDictionary values; 

     if (!string.IsNullOrEmpty(area)) 
     { 
      values = new RouteValueDictionary(new 
      { 
       Area = area, 
       Controller = "Account", 
       Action = "Login" 
      }); 
     } 
     else 
     { 
      values = new RouteValueDictionary(new 
      { 
       Area = "Frontoffice", 
       Controller = "Account", 
       Action = "Login" 
      }); 
     } 

     filterContext.Result = new RedirectToRouteResult(values); 
    } 

} 

第2部分)

要管理的身份BO和FO,即通過User.Identity得到所有人的正確身份,取決於你所在的區域,在Global.asax中創建了這種方法。

protected void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
     if (Request.IsAuthenticated) 
     { 
      var user = (System.Security.Claims.ClaimsPrincipal)User; 

      if (user.Identities.Count() > 1) 
      { 
       if (HttpContext.Current.Request.Url.ToString().Contains("Frontoffice")) 
       { 
        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(user.Identities.First(), new string[] { "Admin" }); 
       } 
       else 
       { 
        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(user.Identities.ElementAt(1), new string[] { "Admin" }); 
       } 
      } 
     } 
    } 

只要改變,如果任何你想要管理用戶身份何時做(Request.IsAuthentifacted)和BBBBOOOMMMM :)

+0

嗨佩德羅,我面臨着與你類似的情況,但我使用相同的數據庫表來保存前端和後端用戶的記錄。在你的解決方案中,有一件事我不太明白,在我看來,你更新了委託人的角色,並將更新後的一個分配給上下文中的User屬性,但是,你需要做的是拒絕管理員用戶日誌通過FrontOffice進入。根據你的代碼,如果一個管理員用戶登錄到Front Office,他仍然應該以管理員身份登錄。 – VincentZHANG

+0

此外,用戶的意思是什麼。身份有多個要素?非常感謝。 – VincentZHANG