.NET 3.5以降では、System.DirectoryServices.AccountManagement
(S.DS.AM)名前空間をチェックアウトする必要があります。ここではそれについてのすべてを読む:
基本的に、あなたはドメインコンテキストを定義し、簡単にADのユーザーおよび/またはグループを見つけることができます:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// update the properties you need to
user.DisplayName = "Joe Blow";
user.Description = "Some description";
// save back your changes
user.Save();
}
新しいS.DS.AMにより、ADのユーザーやグループで簡単に遊ぶことができます。
あなたは、ユーザーの全体の束を検索する必要がある場合は、あなたがあなたの検索を行うことPrincipalSearcher
と「例による問合せ」プリンシパルを使用することができます。
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
UserPrincipal user = found as UserPrincipal;
if(user != null)
{
// update the properties you need to
user.DisplayName = "Joe Blow";
user.Description = "Some description";
// save back your changes
user.Save();
}
}
あなたは上の任意のプロパティを指定することができますUserPrincipal
を入力し、PrincipalSearcher
の場合は「例によるクエリ」として使用してください。
お返事ありがとうございます。 AccountManagementについて聞いたことがありますが、誰かがアクセスできる属性に非常に制限されていると教えてくれましたか?私は拡張属性に多くのデータを保存していますが、私はまだS.DS.AMを使用できますか? – elwis
@elwis:私がリンクした記事を読んでください! 'UserPrincipal'クラスをあなたのニーズに合わせて拡張することができます。はい、あなたはまだあなたの仕事にS.DS.AMを使用することができます!他のすべてが失敗した場合、 '.GetUnderlyingObject()'を呼び出して、 'UserPrincipal'に属する' DirectoryEntry'を手にして、そこで拡張属性の更新を処理できます。 –
コメントするHav。私はその解決策と一緒に行くことができませんでしたが、私はまだあなたの記事を読んで、それは本当に素晴らしい解決策であるようです。うまくいけば次回、リンクありがとう – elwis