私はこの種の質問が以前に尋ねられたことを知っていますが、他の方法が今私を失敗させています。
現状では、LDAP(つまり、LDAP://10.32.16.80)と、検索するADサーバー内のユーザーグループのリストを指定して、WindowsサービスがADをポーリングします。それらの指定されたグループ内のすべてのユーザーを取得し、それらのグループでさらに多くのグループを繰り返し検索します。次に、各ユーザーは別のアプリケーション認証済みユーザーリストに追加されます。
アプリケーションのこの部分は正常に実行されています。ただし、各ユーザーのわかりやすいドメイン名(つまり、ログインドメイン/ユーザー名の一部)が必要です。
したがって、TESTドメインの一部であるSteveという名前のユーザーがいる場合:TEST/steveが彼のログインです。ADでスティーブを見つけることができますが、彼のAD情報と一緒に「TEST」を保存する必要もあります。
繰り返しになりますが、ディレクトリサーチャーと指定されたLDAP IPを使用して「スティーブ」を正常に見つけることができますが、LDAP IPが与えられた場合、フレンドリドメイン名を見つけるにはどうすればよいですか?
次のコードを試してみると、「defaultNamingContext」にアクセスしようとするとエラーが発生します。
System.Runtime.InteropServices.COMException(0x8007202A):認証メカニズムが不明です。
コードは次のとおりです。
private string SetCurrentDomain(string server)
{
string result = string.Empty;
try
{
logger.Debug("'SetCurrentDomain'; Instantiating rootDSE LDAP");
DirectoryEntry ldapRoot = new DirectoryEntry(server + "/rootDSE", username, password);
logger.Debug("'SetCurrentDomain'; Successfully instantiated rootDSE LDAP");
logger.Debug("Attempting to retrieve 'defaultNamingContext'...");
string domain = (string)ldapRoot.Properties["defaultNamingContext"][0]; //THIS IS WHERE I HIT THE COMEXCEPTION
logger.Debug("Retrieved 'defaultNamingContext': " + domain);
if (!domain.IsEmpty())
{
logger.Debug("'SetCurrentDomain'; Instantiating partitions/configuration LDAP entry");
DirectoryEntry parts = new DirectoryEntry(server + "/CN=Partitions,CN=Configuration," + domain, username, password);
logger.Debug("'SetCurrentDomain'; Successfully instantiated partitions/configuration LDAP entry");
foreach (DirectoryEntry part in parts.Children)
{
if (part.Properties["nCName"] != null && (string)part.Properties["nCName"][0] != null)
{
logger.Debug("'SetCurrentDomain'; Found property nCName");
if ((string)part.Properties["nCName"][0] == domain)
{
logger.Debug("'SetCurrentDomain'; nCName matched defaultnamingcontext");
result = (string)part.Properties["NetBIOSName"][0];
logger.Debug("'SetCurrentDomain'; Found NetBIOSName (friendly domain name): " + result);
break;
}
}
}
}
logger.Debug("finished setting current domain...");
}
catch (Exception ex)
{
logger.Error("error attempting to set domain:" + ex.ToString());
}
return result;
}
編集
提案を試みるためにこのサンプルメソッドを追加しましたが、サーチャーで「FindAll()」呼び出しを押すと「不特定のエラー」という例外が発生します。渡される文字列は次のとおりです: "CN = TEST USER、CN = Users、DC = tempe、DC = ktregression、DC = com"
private string GetUserDomain(string dn)
{
string domain = string.Empty;
string firstPart = dn.Substring(dn.IndexOf("DC="));
string secondPart = "CN=Partitions,CN=Configuration," + firstPart;
DirectoryEntry root = new DirectoryEntry(secondPart, textBox2.Text, textBox3.Text);
DirectorySearcher searcher = new DirectorySearcher(root);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.Filter = "(&(nCName=" + firstPart + ")(nETBIOSName=*))";
try
{
SearchResultCollection rs = searcher.FindAll();
if (rs != null)
{
domain = GetProperty(rs[0], "nETBIOSName");
}
}
catch (Exception ex)
{
}
return domain;