各位最近在研究如何利用HR的用户数据生成AD用户,经过一段时间的研究,总算搞出了点东西,可自动生成用户帐号,用户信息,对于用户的添加,修改,删除都没有问题。但是把用户添加到组的时候出现了问题。我研究了Ad中的组织结构,试图添加删除组织结构但是很是失败。不知道错在什么地方?下面是我写的两个函数:
public bool FindOu(string parent_ou,string dept)
{
#region  查找组织结构
try
{
    DirectoryEntry searchRoot = new DirectoryEntry(parent_ou,this.LoginID,this.Password, AuthenticationTypes.Secure ); string[] props = {"ou"};
DirectorySearcher dirSearch = new DirectorySearcher(searchRoot,"name="+dept,props,SearchScope.OneLevel);
                dirSearch.Filter="(objectClass=organizationalUnit)";
SearchResultCollection cSearchResult=dirSearch.FindAll();
int  s_count=cSearchResult.Count;
searchRoot.Close();
if (s_count>0)
return true;
else
return false;
}
catch
{
return false;

#endregion
}
public bool  CreateOrganizeUnit(string dept,string deptid,string description,string  base_ou,string parentOrganizeUnit,ref string msg)
{
#region 产生新的组织结构
string parent_ou=GetOrganizeNamePath(parentOrganizeUnit);
if (!FindOu(parent_ou,dept))
{
parent_ou=parent_ou.Replace("ou="+dept+",","");
DirectoryEntry parentEntry = new DirectoryEntry(base_ou,this.LoginID,this.Password,AuthenticationTypes.Secure);
DirectoryEntry organizeEntry = parentEntry.Children.Add("ou="+dept,"organizationalUnit");
organizeEntry.Properties["distinguishedname"].Add(parent_ou);
organizeEntry.Properties["name"].Add(dept);
organizeEntry.Properties["ou"].Add(dept);
organizeEntry.Properties["description"].Add(description);
organizeEntry.Properties["postalcode"].Add(deptid);
//organizeEntry.Properties["distinguishedname"].Add(deptid);
organizeEntry.CommitChanges();
//parentEntry.CommitChanges();
msg="新增成功!";
return true;
}
else
{
msg="该部门已经存在!";
return false;
}
#endregion
}
   但系统提示总是出错,各位老大能否指出兄弟我的代码错在什么地方?请您不吝赐教。
   我的msn:[email protected]  e_mail:[email protected]

解决方案 »

  1.   

    查找组织结构都没有什么问题,添加用户到组的函数如下:
      public bool AddToGroup(string sUserLocation,string sGroupLocation)
    {
    #region
    try
    {
    DirectoryEntry grp = new DirectoryEntry(sGroupLocation,this.LoginID,this.Password,AuthenticationTypes.Secure);
    if (grp != null) 
    {
    grp.Invoke("Add", new object[] {sUserLocation}); 
    }
    return true;
    }
    catch(Exception ex)
    {
    this.ErrorMess=ex.Message;
    return false;
    }
    #endregion
    }