単純なシナリオで苦労しています。コンピュータへのログインに使用するユーザー名とパスワードを使用して、Active Directory からアカウントを取得したいと考えています。
最初の問題は、UserPrincipal.FindByIdentity を呼び出そうとしたときに、サーバーから紹介を受けていたことです。PrincipalContext.ValidateCredentials が正常に機能していたという事実を考えると、これは少し奇妙だと思いましたが、DC パスが正しくないことがわかりました。
OU/DC 文字列を適切に作成する方法がわかりませんでした。 そのため、次のコードを提供するのに役立つこのSO投稿を見つけました。
private static string GetDomainControllerString()
{
string pdc;
using (var context = new PrincipalContext(ContextType.Domain))
{
string server = context.ConnectedServer; // "pdc.examle.com"
string[] splitted = server.Split('.'); // { "pdc", "example", "com" }
IEnumerable<string> formatted = splitted.Select(s => String.Format("DC={0}", s));// { "DC=pdc", "DC=example", "DC=com" }
string joined = String.Join(",", formatted); // "DC=pdc,DC=example,DC=com"
// or just in one string
pdc = String.Join(",", context.ConnectedServer.Split('.').Select(s => String.Format("DC={0}", s)));
}
return pdc;
}
このコードを使用して DC 文字列を適切に生成した後、エラー メッセージが変わりました。現在、「サーバー上にそのようなオブジェクトはありません」というエラーが表示されます。問題は、私の OU か、FindByIdentity の呼び出し方法にあると思われます。
取得しようとしているユーザー アカウントの場所は次のとおりです。
そして、これが私がそのユーザーにアクセスしようとしている方法です:
private static void Main(string[] args)
{
const string Domain = "SLO1.Foo.Bar.biz";
const string DefaultOU = "OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz";
const string username = @"sanderso";
const string password = "**********";
var principalContext = new PrincipalContext(ContextType.Domain, Domain, DefaultOU, ContextOptions.Negotiate, username, password);
bool areCredentialsValid = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate);
if (areCredentialsValid)
{
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);
}
}
私も電話してみました:
UserPrincipal.FindByIdentity(principalContext, IdentityType.Name, "Sean Anderson");
UserPrincipal.FindByIdentity(principalContext, "Sean Anderson");
これらは同様に失敗しました。