0

在我的C#MVC4應用程序中,我使用基於窗體的身份驗證與Active Directory。我有一個自定義AD成員資格提供者。我已經測試成功,可以讀取並驗證用戶屬於哪個組。現在,我試着去創建一個自定義授權屬性,該屬性將執行以下操作:MVC4表單身份驗證Active Directory自定義授權屬性

if (user is logged-in/not timed-out/authenticated) 
{ 
    if (user's role is equal to role 1 or role 2) 
     { 
     return a specific view or (preferably) perform a specific redirect to action 
     } 
    else 
     { 
     return a different specific view or (preferably) perform a different specific  redirect to action 
     } 
} 
else 
    {  
    return View 
    } 

這是我到目前爲止有:

public class AuthorizeEditAttribute : AuthorizeAttribute 
    { 
     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      if (httpContext.Request.IsAuthenticated) 
      { 
       if ((httpContext.User.IsInRole("group1")) || (httpContext.User.IsInRole("group2"))) 
       { 

        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      else 
      { 
       return false; 
      } 
} 

我無法弄清楚如何還執行重定向任務。我已經看過這個post討論如何做重定向,但不明白我如何可以將其與我迄今爲止的內容進行整合。特別是因爲我相信我必須使用AuthorizeCore訪問httpcontext.user才能執行第一次檢查,並且我不知道如何傳遞需要的另一個類型爲AuthorizationContext的參數,以執行看起來正沿着期望的路徑傳遞重定向。

+0

請看看它是否能幫助 [http://stackoverflow.com/questions/35120816/mvc4-and-ef5 -with-有源目錄的驗證和 - 角色的在-SQL](http://stackoverflow.com/questions/35120816/mvc4-and-ef5-with-active-directory-authentication-and-roles-in- SQL) – user2988717 2016-02-01 10:57:26

回答

1

我想你也應該覆蓋OnAuthorization方法。這有一個AuthorizationContext參數可能讓你到結果集根據自己的喜好的RedirectResult ...