【问题标题】:base.AuthorizeCore(httpContext) is always falsebase.AuthorizeCore(httpContext) 总是假的
【发布时间】: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


【解决方案1】:

base.AuthorizeCore(httpContext) 基于角色和组工作。您可能没有“DevUsers”组。 添加公共字符串 CustomGroups { get;放; } 上课并这样称呼它:

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

    return View();
}

通过这种方式,您发送组 null 然后 base.AuthorizeCore(httpContext) 只执行身份验证部分,您在自定义函数中执行组和角色。

【讨论】:

    【解决方案2】:

    只需忘记 httpcontext 在方法中得到接收...东西会干扰事物...

    像这样:

    protected override bool AuthorizeCore(HttpContextBase httpContext) {

                string Domain = WebConfigurationManager.AppSettings["Domain"];
                string AdGroups = WebConfigurationManager.AppSettings["AdGroups"];
    
    
                /* Return true immediately if the authorization is not 
                locked down to any particular AD group */
                if (String.IsNullOrEmpty(AdGroups))
                    return true;
    
                // Get the AD groups
                //var groups = Groups.Split(',').ToList();
    
                WindowsIdentity CurrentIdentity = WindowsIdentity.GetCurrent();
                UserPrincipal userPrincipal = UserPrincipal.Current;
    
                var groups = AdGroups.Split(',').ToList();
    
                List<GroupPrincipal> result = new List<GroupPrincipal>();
                PrincipalSearchResult<Principal> groups2 = userPrincipal.GetAuthorizationGroups();
    
                // iterate over all groups
                foreach (Principal p in groups2)
                {
                    // make sure to add only group principals
                    if (p is GroupPrincipal)
                    {
    
                        foreach (var group in groups)
                            try
                            {
    
                                if (p.ToString().Equals(group.ToString()))
                                {
                                    return true;
                                }
                            }
                            catch (NoMatchingPrincipalException ex)
                            {
                            }
    
                        //result.Add((GroupPrincipal)p);
                    }
                }
                return false;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 2015-03-28
      • 2019-10-11
      • 2020-10-13
      • 1970-01-01
      • 2011-06-20
      相关资源
      最近更新 更多