110,561
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.DirectoryServices;
namespace DChelper
{
/// <summary>
/// DCHelper 的摘要说明。
/// </summary>
public class DCHelper
{
public DCHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void CreateOu(string ouname,string parent,string oucode)
{
String RootDSE;
DirectoryEntry myDE=null;
System.DirectoryServices.DirectorySearcher DSESearcher
= new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
if (parent==null)
{
myDE = new DirectoryEntry(RootDSE);
}
else
{
DSESearcher=new DirectorySearcher(RootDSE);
DSESearcher.Filter="(&(objectClass=organizationalUnit)(ou=" + parent + "))";
SearchResult mySearchResult = DSESearcher.FindOne();
myDE = mySearchResult.GetDirectoryEntry();
}
DirectoryEntries myEntries = myDE.Children;
DirectoryEntry myDirectoryEntry =
myEntries.Add("OU=" + ouname,"organizationalUnit");
myDirectoryEntry.Properties["name"].Value=ouname;
myDirectoryEntry.Properties["ou"].Value=ouname;
myDirectoryEntry.Properties["postalCode"].Value=oucode;
myDirectoryEntry.CommitChanges();
}
public void CreateUser(string account,string strPwd,string ouname,string displayname)
{
DirectoryEntry obDirEntry = null;
try
{
String RootDSE;
System.DirectoryServices.DirectorySearcher DSESearcher
= new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
if(ouname==null)
{
obDirEntry = new DirectoryEntry(RootDSE);
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add("CN=" + account, "user");
obUser.Properties["company"].Value="暨南大学附属第一医院";
obUser.Properties["department"].Value=ouname;
obUser.Properties["mail"].Value=account+"@jd120.com";
//obUser.Properties["title"].Value=zhicheng;
obUser.Properties["displayname"].Value=displayname;
obUser.Properties["samAccountName"].Value=account;
obUser.Properties["userAccountControl"][0] = 0X10000 | 0X0200;//启用用户
//object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
else
{
DSESearcher=new DirectorySearcher(RootDSE);
DSESearcher.Filter="(&(objectClass=organizationalUnit)(ou=" + ouname + "))";
SearchResult mySearchResult = DSESearcher.FindOne();
if(mySearchResult.GetDirectoryEntry()!=null)
{
obDirEntry = mySearchResult.GetDirectoryEntry();
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add("CN=" + account, "user");
obUser.Properties["company"].Value="暨南大学附属第一医院";
obUser.Properties["department"].Value=ouname;
obUser.Properties["mail"].Value=account+"@jd120.com";
//obUser.Properties["title"].Value=zhicheng;
obUser.Properties["displayname"].Value=displayname;
obUser.Properties["samAccountName"].Value=account;
obUser.Properties["userAccountControl"].Value = 0X10000 | 0X0200;//启用用户
//object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}