質問

system.directoryservices.ccountmanagement .NETライブラリのAccountManagement部分を使用して、ActiveDirectoryにインターフェイスします。

getmembers()にコープリンシパルのオブジェクトに電話をかけ、結果をフィルタリングしたので、ユーザープリンシャルオブジェクトのコレクションがあります

GroupPrincipal myGroup;  // population of this object omitted here 

foreach (UserPrincipal user in myGroup.GetMembers(false).OfType<UserPrincipal>())
{
    Console.WriteLine(user.SamAccountName);
}

上記のコードサンプルには、「testuser1」などのユーザー名が印刷されます。これらを「domain testuser1」形式の別のアプリケーションからのリストと比較する必要があります。

userprincipalオブジェクトから「ドメイン」部分を取得するにはどうすればよいですか?

複数のドメインが関与しているため、既知のドメイン名を追加することはできません。Domain1 testuser1とdomain2 testuser2を区別する必要があります。

役に立ちましたか?

解決

私が考えることができる2つの選択肢があります。

  1. 解析する、またはそのすべてを取る、の権利 name@fully.qualified.domain.name;
  2. 使用 System.DirectoryServices 名前空間。

私は知りません ユーザープリンシパル, 、私もそうではありません Gropprincipal. 。一方、私はあなたが望むものを達成するための実用的な方法を知っています。

[TestCase("LDAP://fully.qualified.domain.name", "TestUser1")] 
public void GetNetBiosName(string ldapUrl, string login)
    string netBiosName = null;
    string foundLogin = null;

    using (DirectoryEntry root = new DirectoryEntry(ldapUrl))
        Using (DirectorySearcher searcher = new DirectorySearcher(root) {
            searcher.SearchScope = SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("sAMAccountName");
            searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", login);

            SearchResult result = null;

            try {
                result = searcher.FindOne();

                if (result == null) 
                    if (string.Equals(login, result.GetDirectoryEntry().Properties("sAMAccountName").Value)) 
                        foundLogin = result.GetDirectoryEntry().Properties("sAMAccountName").Value
            } finally {
                searcher.Dispose();
                root.Dispose();
                if (result != null) result = null;
            }
        }

    if (!string.IsNullOrEmpty(foundLogin)) 
        using (DirectoryEntry root = new DirectoryEntry(ldapUrl.Insert(7, "CN=Partitions,CN=Configuration,DC=").Replace(".", ",DC=")) 
            Using DirectorySearcher searcher = new DirectorySearcher(root)
                searcher.Filter = "nETBIOSName=*";
                searcher.PropertiesToLoad.Add("cn");

                SearchResultCollection results = null;

                try {
                    results = searcher.FindAll();

                    if (results != null && results.Count > 0 && results[0] != null) {
                        ResultPropertyValueCollection values = results[0].Properties("cn");
                        netBiosName = rpvc[0].ToString();
                } finally {
                    searcher.Dispose();
                    root.Dispose();

                    if (results != null) {
                        results.Dispose();
                        results = null;
                    }
                }
            }

    Assert.AreEqual("INTRA\TESTUSER1", string.Concat(netBiosName, "\", foundLogin).ToUpperInvariant())
}

これで利用可能な他の関連情報またはリンクは質問です。
C#Active Directory:ユーザーのドメイン名を取得しますか?
ドメインのnetbios名を見つける方法

他のヒント

Activeds comライブラリを使用してください。これは、機能していない名前の翻訳が組み込まれており、仮定しない(ここで他の回答のように)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActiveDs;

namespace Foo.Repository.AdUserProfile
{
    public class ADUserProfileValueTranslate
    {
        public static string ConvertUserPrincipalNameToNetBiosName(string userPrincipleName)
        {
            NameTranslate nameTranslate = new NameTranslate();
            nameTranslate.Set((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME, userPrincipleName);
            return nameTranslate.Get((int) ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4);
        }
    }
}

完全に資格のあるドメイン名をこの他のアプリに渡してみましたか?ほとんどのWindows APIは、もしそうであれば文句を言いません fully_qualified_domain\USER.

user.distinguednameプロパティで可能なドメインを探すことができます。ドメイン1のユーザーには、文字列「dc = domain1」を含める必要があります。文字列「dc = domain2」を含めるべきではありません。

質問へのコメントの1つで述べたように、これは最近の時代の良い答えだと思います。

 user.Sid.Translate(typeof(System.Security.Principal.NTAccount)).ToString()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top