【问题标题】:HttpContext.Current.Request.LogonUserIdentity.Groups returns different resultsHttpContext.Current.Request.LogonUserIdentity.Groups 返回不同的结果
【发布时间】:2012-11-30 05:49:24
【问题描述】:

我正在尝试使用 Windows 身份验证在我的 MVC Intranet 应用程序中的特定控制器上使用 MVC Authorize 属性。 IIS 7.5 设置为仅使用 Windows 身份验证,并且在 web.config 中禁用匿名访问。我通过了域的身份验证,但是,当对控制器执行任何操作时,我仍然会收到输入凭据的提示。我检查了我的浏览器设置 (IE9),它被设置为使用我当前的 Windows 凭据自动登录。

我尝试创建一个自定义的 Authorize Attribute 类来查看发生了什么。在 AuthorizeCore 中,我使用 httpContext 检查了我的用户名和组成员身份。我发现我所属的 System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups 中缺少一个组。一旦 authorizecore 返回 false 并出现凭据提示,我将提供与我当前登录时使用的凭据相同的凭据,然后 AuthorizeCore 将再次运行。这一次,找到了所有适当的组,基础 AuthorizeCore 当然授权用户并且一切正常。这是我创建的自定义授权类

public class MyAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        bool chk = httpContext.User.IsInRole("mydomain\\heavyequipadmin");

        // this is just to see what AD groups are provided for the current user   
        ArrayList groups = new ArrayList();
        foreach (System.Security.Principal.IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
        {
            groups.Add(group.Translate(typeof(System.Security.Principal.NTAccount)).ToString());
        }

        // just run the base method to Authorize 
        return base.AuthorizeCore(httpContext);
    }
}

我整个控制器的属性是:

[MyAuthorize(Roles = "mydomain\\HeavyEquipAdmin")]

另外一点,web.config 中的角色管理器是:

 <roleManager defaultProvider="AspNetWindowsTokenRoleProvider" />

关于这应该如何工作,我有什么不明白的地方吗?我只是希望我的身份验证/授权由 AD 处理。我是 MVC 的新手,并且诚实地配置了任何身份验证/授权方案。我确实下载了 MVC 3 的源代码来查看 AuthorizeAttribute 类的代码,看看是否有对我有意义的东西。如有任何建议,将不胜感激!

【问题讨论】:

    标签: asp.net-mvc active-directory authorization authorize-attribute


    【解决方案1】:

    匿名用户在这里扮演一个角色: 如果在 IIS 上匿名用户我们允许,则 System.Web.HttpContext.Current.Request.LogonUserIdentity.Name 将在 IIS 中配置为匿名用户。 否则它需要当前登录的用户。

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 2019-06-03
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      相关资源
      最近更新 更多