Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从DMZ访问Active Directory用户组_C#_Active Directory_Dmz - Fatal编程技术网

C# 从DMZ访问Active Directory用户组

C# 从DMZ访问Active Directory用户组,c#,active-directory,dmz,C#,Active Directory,Dmz,我有一个方法可以检查用户是否是广告组的成员。我尝试使用我自己的AD帐户作为凭据,然后获得有关userprincipal的一些信息,如电子邮件等。但在访问userprincipal组时,我收到以下错误消息: 例外情况: 消息:服务器无法运行。 来源:System.DirectoryServices.AccountManagement TARGETSITE:System.DirectoryServices.AccountManagement.ResultSet GetGroupsMemberOf(S

我有一个方法可以检查用户是否是广告组的成员。我尝试使用我自己的AD帐户作为凭据,然后获得有关userprincipal的一些信息,如电子邮件等。但在访问userprincipal组时,我收到以下错误消息:

例外情况:
消息:服务器无法运行。
来源:System.DirectoryServices.AccountManagement
TARGETSITE:System.DirectoryServices.AccountManagement.ResultSet GetGroupsMemberOf(System.DirectoryServices.AccountManagement.Principal)

STACKTRACE:
在System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOf(Principal p)
at Authorization.AuthorizeAD.IsMemberOfGroup(字符串用户)
位于PVM.Controllers.SecurityController.IsMemberOfGroup(字符串用户)

InnerException:System.Runtime.InteropServices.COMException(0x8007203A):服务器无法运行

位于System.DirectoryServices.PropertyValueCollection.PopulateList()
位于System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry条目,字符串propertyName)
在System.DirectoryServices.PropertyCollection.get_项(字符串propertyName)
在System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()上 在System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsForestName()上 位于System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOf(主体p)

代码:

我认为这可能是权限问题,但当从服务器使用时,查询active directory没有问题

一切都很好。我曾尝试更改IIS应用程序池登录等,但现在我将凭据与PrincipalContext对象一起发送


没有人知道我在这里遗漏了什么吗?

通过使用PrincipalSearcher而不是UserPrincipal.IsMemberOf解决了这个问题,然后我做了我自己的IsMemberOf()

public bool IsMemberOfGroup(string user) {
    using (var context = new PrincipalContext(ContextType.Domain, ContextName, ContextContainer, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer, "myUsername", "myPass")) {
        using (var userPrincipal = UserPrincipal.FindByIdentity(
                context,
                IdentityType.SamAccountName,
                user)) {
            //I can access userPrincipal.DisplayName etc
                var groupName = "TestGroup"
                //This is where I get the error
                return userPrincipal.IsMemberOf(context, IdentityType.SamAccountName, groupName);
            }
        }

        return false;
    }
private static bool IsMemberOf(PrincipalContext context, PrincipalSearcher searcher, string user,
        string groupToFind) {
        searcher.QueryFilter = new GroupPrincipal(context, groupToFind);

        var group = searcher.FindOne() as GroupPrincipal;
        if (group == null) {
            return false;
        }

        if (group.GetMembers()
            .Select(member => member as UserPrincipal)
            .Where(principal => !string.IsNullOrEmpty(principal?.SamAccountName))
            .Any(principal => principal.SamAccountName.Equals(user))) {
            return true;
        }

        return false;
    }