我提到了數百個帖子,但沒有運氣。 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();
}
我們需要一些環境來幫助你。請閱讀本sscce.org – reggaeguitar
什麼錯誤頁面?什麼是錯誤? –
你的問題很混亂,沒有多大意義。你怎麼能使用表單身份驗證,但沒有登錄頁面?你如何期待你的用戶登錄?您似乎在您的Authorize屬性中使用了Active Directory代碼,如果您使用的是Forms身份驗證,則這沒什麼意義。 –