15

ユーザーが特定の親OUに属しているかどうかを確認したいと思います。

どうやってやるの?

私が探しているものの明確な説明については、以下のコードを確認してください。

using System.DirectoryServices.AccountManagement;

public bool IsUserInOU(string samAccountName, string OUName){

    using (var context = new PrincipalContext(ContextType.Domain))
        {
            using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName))
            {                    
                //Check if the user is in the OU specified in OUName
                //Something like:
                //return user.IsInOU(OUName);
            }
         }
}

public void TestIt_1(){
  //The parent OU of this user is "AwesomeOU"
  string samAccountName = "Joe";
  string OUName = "AwesomeOU";
  bool expected = true;
  bool actual = IsUserInOU(samAccountName, OUName);
  Assert.AreEqual(expected, actual);
}

public void TestIt_2(){
  //The parent OU of this user is "WhateverOU"
  string samAccountName = "Mike";
  string OUName = "AwesomeOU";
  bool expected = false;
  bool actual = IsUserInOU(samAccountName, OUName);
  Assert.AreEqual(expected, actual);
}

ドメイン:

  • 全国OU
    • 素晴らしいOU
      • ジョー
    • OUが何であれ
      • マイク

empiの答えの後の解決策1

empiから提供された情報を使用して、DistinguishedNameの最初のOUを抽出するために以下のメソッドを作成しました。それが済んだら、残りは簡単です。

public static string GetOUForUser(string samAccountName)
    {
        using (var context = new PrincipalContext(ContextType.Domain))
        {
            using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName))
            {
                //System.Console.WriteLine(user.DistinguishedName);
                int startIndex = user.DistinguishedName.IndexOf("OU=", 1) + 3; //+3 for  length of "OU="
                int endIndex = user.DistinguishedName.IndexOf(",", startIndex);
                var group = user.DistinguishedName.Substring((startIndex), (endIndex - startIndex));
                return group;
            }
        }
    }

JPBlancの回答後の解決策2

public static string GetOUForUser(string samAccountName)
    {
        using (var context = new PrincipalContext(ContextType.Domain))
        {
            using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, samAccountName))
            {
                using (DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry)
                {
                    using (DirectoryEntry deUserContainer = deUser.Parent)
                    {
                        return deUserContainer.Properties["Name"].Value.ToString();
                    }
                }
            }
        }
    }
4

3 に答える 3

17

わかりました @Empi ソリューションは機能していますが、文字列の方法を使用せずに、探しているオブジェクトを提供するまたはプロパティを提供するオブジェクトにUserPrincipal基づいて構築されています。DirectoryEntryparentcontainer

/* Retreiving a principal context
 */
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "dom\\jpb", "MyPwd");

/* Retreive a user
 */
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, "user1");

/* Retreive the container
 */
DirectoryEntry deUser = user.GetUnderlyingObject() as DirectoryEntry;
DirectoryEntry deUserContainer = deUser.Parent;
Console.WriteLine (deUserContainer.Properties["distinguishedName"].Value);
于 2012-04-13T07:09:28.130 に答える
2

この情報はUserPrincipal.DistinguishedNameにあります。DistinguishedName が "," + ou 識別名 (大文字と小文字を区別しない) で終わっているかどうかを確認する必要があります。ただし、チェックしている ou の識別名を知っている必要があります。

たとえば、 dn が:CN=Jeff Smith,OU=Sales,DC=Fabrikam,DC=COMの場合、ユーザーはOU=Sales,DC=Fabrikam,DC=COMou にいることになります。

于 2012-04-12T11:04:02.080 に答える