2015-04-30 46 views
1

我提到了數百個帖子,但沒有運氣。 base.AuthorizeCore(httpContext)總是返回false。base.AuthorizeCore(httpContext)永遠是假的

我使用IIS Express從VS2012運行MVC應用程序。許多人可以使用基於表單的身份驗證來解決此問題。但我也嘗試過。 請幫忙..

以下是AuthorizeADAttribute是我正在使用的。

public class AuthorizeADAttribute : AuthorizeAttribute 
{ 
    public string Groups { get; set; } 

    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (base.AuthorizeCore(httpContext)) 
     { 

     // var authorized = (httpContext.User.Identity.IsAuthenticated); 


      /* Return true immediately if the authorization is not 
      locked down to any particular AD group */ 
      if (String.IsNullOrEmpty(Groups)) 
       return true; 

      // Get the AD groups 
      var groups = Groups.Split(',').ToList<string>(); 

      // Verify that the user is in the given AD group (if any) 
      var context = new PrincipalContext(ContextType.Domain, "MYDOMAIN"); 
      var userPrincipal = UserPrincipal.FindByIdentity(context, 
               IdentityType.SamAccountName, 
               httpContext.User.Identity.Name); 

      foreach (var group in groups) 
      { 
       try 
       { 
        if (userPrincipal.IsMemberOf(context, IdentityType.Name, group)) 
         return true; 
       } 
       catch (NoMatchingPrincipalException exc) 
       { 
        var msg = String.Format("While authenticating a user, the operation failed due to the group {0} could not be found in Active Directory.", group); 
        System.ApplicationException e = new System.ApplicationException(msg, exc); 
        // ErrorSignal.FromCurrentContext().Raise(e); 
        return false; 
       } 
       catch (Exception exc) 
       { 
        var msg = "While authenticating a user, the operation failed."; 
        System.ApplicationException e = new System.ApplicationException(msg, exc); 
        //ErrorSignal.FromCurrentContext().Raise(e); 
        return false; 
       } 
      } 
     } 
     return false; 
    } 
} 

我正在像這樣傳遞組名。當我使用IIS Express從VS2012運行應用程序時,這是完美的。 web.config文件設置爲 在IIS設置中啓用了基於表單的身份驗證。但URL重定向轉到login.aspx。我沒有我的應用程序中的任何登錄頁面

但是,當我發佈網站到IIS。出現錯誤頁面。

[AuthorizeAD(Groups = "DevUsers")] 
public ActionResult Index() 
{ 

    return View(); 
} 
+0

我們需要一些環境來幫助你。請閱讀本sscce.org – reggaeguitar

+0

什麼錯誤頁面?什麼是錯誤? –

+0

你的問題很混亂,沒有多大意義。你怎麼能使用表單身份驗證,但沒有登錄頁面?你如何期待你的用戶登錄?您似乎在您的Authorize屬性中使用了Active Directory代碼,如果您使用的是Forms身份驗證,則這沒什麼意義。 –

回答

0

base.AuthorizeCore(httpContext)基於角色和組工作。你可能沒有一羣'DevUsers'。 添加公共字符串CustomGroups {get;組; }類,並調用它是這樣的:在這種方式,你把小組null,則base.AuthorizeCore

[AuthorizeAD(CustomGroups = "DevUsers")] 
public ActionResult Index() 
{ 

    return View(); 
} 

(HttpContext的)只是做了身份驗證部分,你在你的自定義函數執行組和角色。