C# InteropServices.COMException

C# InteropServices.COMException,c#,asp.net-mvc,active-directory,C#,Asp.net Mvc,Active Directory,我的MVC项目有问题 堆栈如下所示: private static List<string> GetGroup(SearchResult result) { List<string> nombresPerfilAD = new List<string>(); foreach (var i in result.Properties["memberOf"]) { var group = new DirectoryEntry

我的MVC项目有问题

堆栈如下所示:

private static List<string> GetGroup(SearchResult result)
{
    List<string> nombresPerfilAD = new List<string>();

    foreach (var i in result.Properties["memberOf"])
    {
        var group = new DirectoryEntry(@"LDAP://" + i);      
        nombresPerfilAD.Add(group.Name.Split('=')[1].ToUpper().Trim());             
    }

    return nombresPerfilAD;
}  
System.Runtime.InteropServices.COMException(0x80005000):System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)处System.DirectoryServices.DirectoryEntry.Bind()处System.DirectoryServices.DirectoryEntry.get_Name()处的未知错误(0x80005000),位于PosmanWeb2.Controller.Helpers.SessionHelpers.GetGroup处(SearchResult result)位于PosmanWeb2.Controllers.Helpers.SessionHelpers.getperfild(SearchResult result)位于PosmanWeb2.Controllers.Helpers.SessionHelpers.ConnectActiveDirectory(),位于PosmanWeb2.Controllers.Helpers.SessionHelpers.ConnectActiveDirectory()

SessionHelper上的方法是项目的一部分,最后一个方法似乎有问题,代码如下:

private static List<string> GetGroup(SearchResult result)
{
    List<string> nombresPerfilAD = new List<string>();

    foreach (var i in result.Properties["memberOf"])
    {
        var group = new DirectoryEntry(@"LDAP://" + i);      
        nombresPerfilAD.Add(group.Name.Split('=')[1].ToUpper().Trim());             
    }

    return nombresPerfilAD;
}  
private静态列表GetGroup(SearchResult)
{
List nombresperfild=新列表();
foreach(result.Properties[“memberOf”]中的变量i)
{
var group=newdirectoryEntry(@“LDAP://”+i);
nombresperfild.Add(group.Name.Split('=')[1].ToUpper().Trim());
}
返回nombresperfild;
}  
它的基本功能是将所有Active Directory配置文件保存在一个列表中

特别是一个用户没有这个问题,而另外两个用户正好有这个问题


我在其他线程上看到它可能与权限问题有关,但我不能100%确定要查找哪里。

当前范围内的一个好解决方案是不绑定到Active Directory.result.Properties[“memberOf”]已包含组DN。您可以使用IADsPathName接口从中获取名称,而无需连接到AD(需要添加对ActiveDs com对象的引用)。此外,您还可以使用此接口取消特殊字符的转义。例如,如果组名为#Test,它将以转义形式返回,如\#Test。因此,如果您以以下方式重写方法,您将解决问题,提高性能并处理字符转义:

    private static List<string> GetGroup(SearchResult result)
    {
        List<string> nombresPerfilAD = new List<string>();
        IADsPathname pathname = new PathnameClass();
        pathname.SetDisplayType(2);
        pathname.EscapedMode = 4;

        foreach (string groupDn in result.Properties["memberOf"])
        {
            pathname.Set(groupDn, 4);
            nombresPerfilAD.Add(pathname.GetElement(0).ToUpper());
        }

        return nombresPerfilAD;
    }
private静态列表GetGroup(SearchResult)
{
List nombresperfild=新列表();
IADsPathname路径名=新路径名类();
SetDisplayType(2);
pathname.EscapedMode=4;
foreach(result.Properties[“memberOf”]中的字符串groupDn)
{
路径名.Set(groupDn,4);
nombresperfild.Add(路径名.GetElement(0.ToUpper());
}
返回nombresperfild;
}
似乎绑定而不是按逗号拆分是为了避免组名中包含逗号的情况。但是,如果组名包含“=”字符怎么办?旧代码将无法工作

另外,如果使用.NET 4,则需要在ActiveDs属性(在项目引用中)中将嵌入互操作类型设置为false


请注意,memberOf属性不包含嵌套组(userA->groupA,groupA->groupB=>userA->groupB)和用户的主组

当前范围内的一个好解决方案是不绑定到Active Directory.result.Properties[“memberOf”]已包含组DN。您可以使用IADsPathName接口从中获取名称,而无需连接到AD(需要添加对ActiveDs com对象的引用)。您还可以使用此接口取消特殊字符的转义。例如,如果您的组名为#Test,它将像#Test一样作为转义返回。因此,如果您以以下方式重写方法,您将解决问题,提高性能并处理字符转义:

    private static List<string> GetGroup(SearchResult result)
    {
        List<string> nombresPerfilAD = new List<string>();
        IADsPathname pathname = new PathnameClass();
        pathname.SetDisplayType(2);
        pathname.EscapedMode = 4;

        foreach (string groupDn in result.Properties["memberOf"])
        {
            pathname.Set(groupDn, 4);
            nombresPerfilAD.Add(pathname.GetElement(0).ToUpper());
        }

        return nombresPerfilAD;
    }
private静态列表GetGroup(SearchResult)
{
List nombresperfild=新列表();
IADsPathname路径名=新路径名类();
SetDisplayType(2);
pathname.EscapedMode=4;
foreach(result.Properties[“memberOf”]中的字符串groupDn)
{
路径名.Set(groupDn,4);
nombresperfild.Add(路径名.GetElement(0.ToUpper());
}
返回nombresperfild;
}
似乎绑定而不是按逗号拆分是为了避免组名中包含逗号的情况。但是,如果组名包含“=”字符怎么办?旧代码将无法工作

另外,如果使用.NET 4,则需要在ActiveDs属性(在项目引用中)中将嵌入互操作类型设置为false


注意,memberOf属性不包含嵌套组(userA->groupA,groupA->groupB=>userA->groupB)和用户的主要组

尝试更改代码并使用
PrincipalContext
您可以在AD中访问组,而无需使用旧的LDAP方法尝试更改代码并使用
PrincipalContext
您可以在AD中访问组,而无需使用旧的LDAP方法。我正在尝试查找ActiveDs com对象,哪一个是是吗?我有以下内容:活动DS IIS扩展Dll活动DS IIS扩展命名空间提供程序活动DS类型库确定我找到了它,它是活动DS类型库,但当我尝试实例化IADsPathname时,它会给我以下错误:互操作性类型“ActiveDs.PathnameClass”无法嵌入,请改为使用aplicable接口。好的,我更改了属性,抱歉我的错误。我正在尝试查找ActiveDs com对象,它是哪一个?我有以下内容:活动DS IIS扩展Dll活动DS IIS扩展命名空间提供程序活动DS类型库确定我找到了它,它是活动DS类型库,但当我尝试实例化时IADsPathname它给了我以下错误:不能嵌入互操作性类型“ActiveDs.PathnameClass”,请使用可应用接口。好的,我更改了属性,很抱歉,我的错误