使用 DirectoryService 更新用户

发布于 2024-12-22 11:23:29 字数 331 浏览 0 评论 0原文

我正在编写一个小型应用程序,它使用数据库表中的信息更新我的 AD,但无法找到最佳实践的示例。

据我了解,我需要:

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

是这样还是我完全丢失了,任何提示或示例都是 欢迎

I'm writing a small application which updates my AD with info from a DB table, but are having trouble finding examples of best practices.

As far as I understand, I'll need to:

  • create a DirectorySearcher with a filter objectClass=user and search for the given cn
  • if found, I need to use result.getDirectoryEntry to get a handle to the actual object,
  • Update all the values to my entryobject with the one's from the db and then commit changes

Is that it or am I totally lost, any hints or examples are welcome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋情葬爱 2024-12-29 11:23:29

如果您使用的是 .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 的“示例查询”。

If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

Basically, you can define a domain context and easily find users and/or groups in 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();
}

The new S.DS.AM makes it really easy to play around with users and groups in AD!

If you need to search a whole bunch of users, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// 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();
    }
}

You can specify any of the properties on the UserPrincipal and use those as "query-by-example" for your PrincipalSearcher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文