【发布时间】:2020-02-14 04:55:13
【问题描述】:
我参考了数百个帖子,但还没有运气。 base.AuthorizeCore(httpContext) 总是返回 false。
我正在使用 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
-
什么错误页面?什么错误?
-
你的问题真的很混乱,没有多大意义。您如何使用表单身份验证,但没有登录页面?您希望您的用户如何登录?您似乎在 Authorize 属性中使用了 Active Directory 代码,如果您使用的是表单身份验证,这几乎没有意义。
-
好吧..我对这个完全陌生..我只是想限制基于AD组的视图页面显示。我的应用程序根本没有任何登录页面。它应该怎么做..?感谢您尝试帮助我。
标签: asp.net-mvc asp.net-mvc-4 authorize-attribute