首页 文章

HttpContext.Current.Request.LogonUserIdentity.Groups返回不同的结果

提问于
浏览
3

我正在尝试使用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当然授权用户,一切正常 . 这是我创建的自定义Authorization类

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类的代码,看看是否有一些对我有用的东西 . 任何建议在这里将不胜感激!

1 回答

  • 3

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

相关问题