0

ASP.NET と Active Directory に問題があります。

ユーザーが Active Directory のグループに属しているかどうかを確認したいのですが、このグループに属している場合は、詳細を表示できます。このために、フィルター文字列を使用して関数を作成します。問題は、当社ではグループを切り替えており、構造が静的ではないことです。このために、最初にグループを検索し、次にパラメーター member-of を使用してグループ内のユーザーを検索します...

ADの構造は次のとおりです。

ここに画像の説明を入力

グループを検索するための私のコードは次のとおりです。

public string GetGroup(string groupname)
        {
            string path = "<OurDomain>";

            DirectoryEntry rootEntry = new DirectoryEntry(path);

            DirectorySearcher srch = new DirectorySearcher(rootEntry);
            srch.SearchScope = SearchScope.Subtree;

            srch.Filter = "(&(objectCategory=Group)(name=" + groupname + "))";

            SearchResult resFilter = srch.FindOne();

            string filterpath = resFilter.Path;

            return filterpath; 
        }

ユーザーを見つけるための私の方法:

public bool IsUserInGroup(string username,string groupepath) 
        {
            string path = "<OurDomain>"; 

            DirectoryEntry rootEntry = new DirectoryEntry(path);

            DirectorySearcher srch = new DirectorySearcher(rootEntry);
            srch.SearchScope = SearchScope.Subtree;

            srch.Filter = "(&(objectClass=user)(sAMAccountName=*" + username + "*)(memberof=CN=GastzugangUser,OU=SubFolderB,OU=FolderB,DC=company,DC=com))";


            SearchResultCollection res = srch.FindAll();

            if (res == null || res.Count <= 0)
            {
                return false;
            }
            else
            {
                return true; 
            }
        }

グループのサブグループとそのダイナミックでユーザーを検索するにはどうすればよいですか? :(

4

2 に答える 2

1

.NET 3.5 以降を使用している場合は、System.DirectoryServices.AccountManagement(S.DS.AM) 名前空間を確認してください。ここでそれについてすべて読んでください:

基本的に、ドメイン コンテキストを定義し、AD でユーザーやグループを簡単に見つけることができます。

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
  // find a user
  UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

  if(user != null)
  {
      // GetAuthorizationGroups returns a list of GroupPrincipals and work recursively
      var groupsForUser = user.GetAuthorizationGroups();

      // then check to see if that group you want it part of this list
  }
}

新しい S.DS.AM を使用すると、AD でユーザーやグループを簡単に操作できます。

于 2013-01-09T12:18:45.720 に答える
1

それを試しませんでしたが、これをフィルターに追加すると役立ちますか? http://ldapwiki.willeke.com/wiki/1.2.840.113556.1.4.1941

例えば

(&(objectClass=user)(sAMAccountName=*" + username + "*)(memberof:1.2.840.113556.1.4.1941:=CN=GastzugangUser,OU=SubFolderB,OU=FolderB,DC=company,DC=com))";
于 2013-01-09T12:12:59.697 に答える