【问题标题】:Unlocking user Account解锁用户帐户
【发布时间】:2011-08-09 19:24:24
【问题描述】:

我正在尝试设置属性以解锁 AD 中的用户帐户,并且我正在使用以下代码;问题是 de 不包含 userAccountControl 并且代码失败。

我可以使用DirectorySearcher 获得userAccountControl 的值,但这对我使用de 设置属性没有帮助。有人可以帮我吗?提前致谢

String m_Path = "LDAP://" + distinguishedName;

using (DirectoryEntry de = new DirectoryEntry(m_Path))
{
   if (de.Contains("userAccountControl")
   {
      int m_Val  = (int)de.Properties["userAccountControl"][0].Value;
      de.Properties["userAccountControl"].Value = m_Val | 0x0001
      de.CommitChanges;
   }
}

【问题讨论】:

    标签: c# .net active-directory


    【解决方案1】:

    我认为您需要检查de.Properties 是否包含userAccountControl 的值!

    string ldapPath = "LDAP://" + distinguishedName;
    
    using(DirectoryEntry de = new DirectoryEntry(ldapPath))
    {
       // check to see if we have "userAccountControl" in the **properties** of de
       if(de.Properties.Contains("userAccountControl")
       {
          int m_Val  = (int)de.Properties["userAccountControl"][0].Value ;
          de.Properties["userAccountControl"].Value = m_Val | 0x0001;
    
          de.CommitChanges();
       }
    }
    

    此外,如果您使用的是 .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)
    {
       // unlock user
       user.UnlockAccount();
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多