.net对ladp操作windowsAD的一些类和接口,都放在System.DirectoryServices命名空间下面。
public void CreateWindowsUser(String userName,String password,String userDesc,int userControl)
{
String ladpRootPath = "LDAP://192.168.213.168/CN=Users,DC=pk1,DC=cctv,DC=com";
DirectoryEntry ladpRoot = new DirectoryEntry(ladpRootPath);
ladpRoot.Username = "XXXXX";
ladpRoot.Password = "XXXXX";
DirectoryEntry user = ladpRoot.Children.Add("CN="+ userName, "user");
user.Properties["sAMAccountName"].Value = userName;
user.Properties["description"].Value = userDesc;
user.Properties["userAccountControl"].Value = userControl;
user.CommitChanges();
user.Invoke("SetPassword", password);
user.CommitChanges();
user.Close();
}
上述方法简单实现了添加一个Windows用户,值得注意的是如果你的程序是放在域控上面执行的,那么 ladpRoot.Username和ladpRoot.Password就不能赋值。另外ladpRootPath是ladp的路径写法,LDAP://192.168.213.168表示你要操作的主机地址, DC=pk1,DC=cctv,DC=com表示要操作的Windows域,CN=Users表示在windows域的主目录下的Users容器对象。方法参数userControl表示一些用户属性,如密码永不过期,禁用用户等等,具体的值,详见http://support.microsoft.com/kb/305144/zh-cn,如果用户包含多个属性,则将这些属性值做按位或(|)的运算即可。在这些用户属性中,有一个比较例外,那就是PASSWD_CANT_CHANGE,此属性表示用户不能修改密码,但是他是一个只读的属性,也就是说不能通过设置userAccountControl来实现功能,如果要实现此功能,那么必须在上述代码 user.Close();之前插入如下代码:
Guid changePasswordGuid = new Guid("{ab721a53-1e2f-11d0-9819-00aa0040529b}");
IdentityReference selfSddl = new SecurityIdentifier(WellKnownSidType.SelfSid, null);
IdentityReference everyoneSddl = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
ActiveDirectoryAccessRule selfAccRule = new ActiveDirectoryAccessRule(selfSddl, ActiveDirectoryRights.ExtendedRight, AccessControlType.Deny, changePasswordGuid);
ActiveDirectoryAccessRule everyoneAccRule = new ActiveDirectoryAccessRule(everyoneSddl, ActiveDirectoryRights.ExtendedRight, AccessControlType.Deny, changePasswordGuid);
user.ObjectSecurity.AddAccessRule(selfAccRule);
user.ObjectSecurity.AddAccessRule(everyoneAccRule);
user.CommitChanges();
下面给出一些常用的用户操作代码:
修改用户:
public void ModifyWindowsUser(String userName,String password,String userDesc,int userControl)
{
String ladpRootPath = "LDAP://192.168.213.168/CN=Users,DC=pk1,DC=cctv,DC=com";
DirectoryEntry ladpRoot = new DirectoryEntry(ladpRootPath);
ladpRoot.Username = "XXXXX";
ladpRoot.Password = "XXXXX";
DirectoryEntry user = ladpRoot.Children.Find("CN=" +userName, "user");
user.Properties["description"].Value = userDesc;
user.Properties["userAccountControl"].Value = userControl;
user.CommitChanges();
user.Invoke("SetPassword", password);
user.CommitChanges();
user.Close();
}
删除用户
public void DeleteWindowsUser(String userName)
{
String ladpRootPath = "LDAP://192.168.213.168/CN=Users,DC=pk1,DC=cctv,DC=com";
DirectoryEntry ladpRoot = new DirectoryEntry(ladpRootPath);
ladpRoot.Username = "XXXXX";
ladpRoot.Password = "XXXXX";
DirectoryEntry user = ladpRoot.Children.Find("CN=" +userName, "user");
user.DeleteTree();
user.Close();
}
分享到:
相关推荐
C#windows编程 (pdf)
c# window服务多线程实例
利用Windows10系统API实现的C#通知,可以实现使用C#推送通知到Win10通知栏,并可以和该通知进行交互。
C#创建Windows服务(Windows Services) 实战之系统定时重启服务-程序开发
使用C#获取WINDOWS系统的图标--DEMO
C#操作Windows Xp系统 C#操作Windows Xp系统 C#操作Windows Xp系统 C#操作Windows Xp系统 C#操作Windows Xp系统 C#操作Windows Xp系统
为《使用C# 完美实现多语言版本Windows应用程序》所附源代码,以便更好的学习C#窗体程序多语言版本的制作。 本源代码可以任何方式免费使用,但转载请注明出处 另:代码中仅处理资源,没有本地格式信息的处理,有这...
C# 开机启动服务(windows service) 本程序用ASP.NET C#开发,不针对系统版本限制。 本源码拿就来可以直接写您想要的服务,无需再改其它的代码,拿来主义是不是很好?例如订单服务。 本服务是100%源码,源码中包含: ...
C#开发的简单的windows Explorer文件管理 能够查看文件夹,文件,运行文件及程序
在WinForm下模拟windows用户访问域,语言为C#
C# 仿WINDOWS 计算器 C# 仿WINDOWS 计算器
使用c#创建windows窗口(基于api)
利用 Windows API 32 中打印函数,对打印机进行发送打印任务、暂停、取消、获取打印任务、获取打印机列表、状态、纸张等信息。压缩包中包含4个平时收集的打印类,测试均可用。附含Windows API 函数 for Visual Basic...
C# 用于操作Windows INI 文件的封装类
主要介绍了Windows系统中使用C#编写蓝牙通信程序的简单实例,文中的例子使用到了32feet.NET中的InTheHand.Net.Personal类库,需要的朋友可以参考下
一个asp.net工程,demo页面上进行域用户的认证,创建,删除操作
详细介绍了 C# 如何操作AD(活动目录) 附有代码Demo
c#类似windowxp界面c#类似windowxp界面
c#消息 windows消息参数大全
利用C# 部署Windows 服务,自动控制相关内容。 常用于程序的自动审核。