KSM*_*KSM 3 .net c# active-directory
我正在尝试设置属性以解锁AD中的用户帐户,我使用以下代码; 问题是de不包含userAccountControl,代码失败.
我可以userAccountControl通过使用获得值,DirectorySearcher但这对我设置属性没有帮助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;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为你需要检查是否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();
}
}
Run Code Online (Sandbox Code Playgroud)
此外,如果您使用的是.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();
}
Run Code Online (Sandbox Code Playgroud)
新的S.DS.AM使得在AD中与用户和群组玩游戏变得非常容易!