Active directory 使用DirectoryService更新用户

Active directory 使用DirectoryService更新用户,active-directory,directoryservices,Active Directory,Directoryservices,我正在编写一个小应用程序,它使用DB表中的信息更新我的广告,但是在寻找最佳实践的示例时遇到了困难 据我所知,我需要: 使用过滤器创建一个目录搜索器,并搜索给定的cn 如果找到,我需要使用result.getDirectoryEntry来获取实际对象的句柄 使用数据库中的值更新myentryobject的所有值,然后提交更改 是它还是我完全迷路了,欢迎您提供任何提示或示例如果您使用的是.NET 3.5及更高版本,您应该查看System.DirectoryServices.AccountMana

我正在编写一个小应用程序,它使用DB表中的信息更新我的广告,但是在寻找最佳实践的示例时遇到了困难

据我所知,我需要:

  • 使用过滤器创建一个目录搜索器,并搜索给定的cn
  • 如果找到,我需要使用
    result.getDirectoryEntry
    来获取实际对象的句柄
  • 使用数据库中的值更新my
    entryobject
    的所有值,然后提交更改

是它还是我完全迷路了,欢迎您提供任何提示或示例

如果您使用的是.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使得在广告中与用户和群组进行互动变得非常容易

如果需要搜索整个用户群,可以使用
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

的“示例查询”。如果您使用的是.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使得在广告中与用户和群组进行互动变得非常容易

如果需要搜索整个用户群,可以使用
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,有人告诉我它非常有限,您可以访问哪些属性?我在extensionattributes中存储了大量数据,我还可以使用S.DS.AM吗?@elwis:阅读我链接到的文章!您可以扩展
UserPrincipal
类以满足您的需要,当然,您仍然可以使用S.DS.AM完成任务!如果所有其他操作都失败,您可以随时调用
.getUnderlineObject()
,并获得属于该
UserPrincipal
DirectoryEntry
,并在那里处理扩展属性的更新。Hav进行注释。我不能接受这个解决方案,但我还是读了你的文章,这似乎真的是一个很好的解决方案。希望下次,谢谢你的链接谢谢你的回答。我听说过AccountManagement,有人告诉我它非常有限,您可以访问哪些属性?我在extensionattributes中存储了大量数据,我还可以使用S.DS.AM吗?@elwis:阅读我链接到的文章!您可以扩展
UserPrincipal
类以满足您的需要,当然,您仍然可以使用S.DS.AM完成任务!如果所有其他操作都失败,您可以随时调用
.getUnderlineObject()
,并获得属于该
UserPrincipal
DirectoryEntry
,并在那里处理扩展属性的更新。Hav进行注释。我不能接受这个解决方案,但我还是读了你的文章,这似乎真的是一个很好的解决方案。希望下次,谢谢你的链接