代码之家  ›  专栏  ›  技术社区  ›  patjbs

FindByIdentity失败,PricipalOperationException在中ASP.NET网络应用

  •  3
  • patjbs  · 技术社区  · 15 年前

    当我尝试验证AD中是否存在提供的用户id时,我使用以下代码进行验证:

    private bool IsWindowsIDValid(string strWindowsID) 
    { 
    var context = new PrincipalContext(ContextType.Domain, "DOMAINSERVER", "DC=DOMAINNAME,DC=net"); 
    var userPrincipal = UserPrincipal.FindByIdentity(context, strWindowsID); 
    return (userPrincipal != null); 
    } 
    

    但是,在第二行抛出一个异常,在这里调用FindByIdentity。以下是例外情况的详细信息:

    信息:

    堆栈跟踪:

    在系统.目录服务.帐户管理.PrincipalContext.DoDomainInit公司() 在系统.目录服务.帐户管理.PrincipalContext.初始化() 在系统.目录服务.帐户管理.获取查询上下文() 在系统.目录服务.帐户管理.Principal.FindByIdentityWithTypeHelper(PrincipalContext context,Type principalType,Nullable`1 identityType,String identityValue,DateTime refDate) 在系统.目录服务.帐户管理.Principal.FindByiIdentityWithType(PrincipalContext上下文,类型principalType,字符串identityValue) 在系统.目录服务.帐户管理.UserPrincipal.FindByIdentity属性(PrincipalContext上下文,字符串标识值) 在*****.IsWindowsIDValid(字符串strWindowsID)处*****。ascx.cs公司:第193行

    如果我尝试检查PrincipalContext的ConnectedServer属性,也会出现同样的错误。但是,我可以尝试根据上下文验证凭据(使用context.ValidateCredentials文件()),一切都会过去的。

    对可能发生的事有什么看法吗?我可以在我的机器上的独立控制台脚本中很好地运行这段代码—这发生在我的本地开发环境中,在VisualStudio内部,当我尝试调试webapp时。这是权限问题还是其他问题?我现在很迷茫。

    -帕特里克

    2 回复  |  直到 15 年前
        1
  •  5
  •   James King    14 年前

    一个老问题,但我有同样的错误。对我来说,问题是 PrincipalContext 如果它的构造函数中没有用户名和密码就无法工作。。。无论何时调用 UserPrincipal 主上下文

    如果指定对指定容器具有Active Directory权限的域用户的用户名和密码,则调用 FindByIdentity 如果成功:

    var context = new PrincipalContext(ContextType.Domain, "DOMAINSERVER",
                                       "DC=DOMAINNAME,DC=net", userName, pw); 
    var userPrincipal = UserPrincipal.FindByIdentity(context, strWindowsID); 
    

    对我来说,这不是一个解决方案,因为我没有这两个参数。但这就是为什么你会犯错误。

    根据微软的帮助,按自己的方式做应该在调用进程的凭据下运行。。。但不管我是谁(我已经验证了模拟),我都会打电话给 用户主体 对象而不指定其上的用户名和密码 只是行不通。

    希望迟来的帮助,

        2
  •  0
  •   David Barrows    11 年前

    对一个老问题的另一个姗姗来迟的回答,但本文在我的案例中帮助我解决了这个问题: http://support.microsoft.com/kb/329986

    我得到的“操作错误”与这篇文章中的类似。

    private static void Main(string[] args)
        {
    
            var adGroups = new List<string>();
    
            using (var principalContext = new PrincipalContext(ContextType.Domain))
            {
                using (var user = UserPrincipal.FindByIdentity(principalContext, @"MYDOMAIN\MYUSERNAME"))
                {
                    if (user == null) return;
    
                    var groups = user.GetAuthorizationGroups();
                    adGroups.AddRange(from @group in groups
                                      where @group.Name.ToUpper().Contains("SOME-STRING-COMMON-TO-ALL-THE-AD-GROUPS-PERTINENT-TO-MY-MVC-APP")
                                      select @group.Name);
                }
            }
        }
    

    这是有效的,所以我确信我的问题是微软文章中描述的“双跳”问题。

    <system.web>
        <httpRuntime targetFramework="4.5" />
        <compilation debug="true" targetFramework="4.5" />
        <authentication mode="Windows" />
        <identity impersonate="true" />
        <authorization>
                <deny users="?" />
        </authorization>