2017-02-23 48 views
0

我真的Active Directory中的新手....如何處理DirectoryEnter.CommitChange()異常? C#

當我檢查,並約Add User And Department到Active Directory使用C#編輯應用程序,

DirectoryEnter.CommitChage()使一個例外是這樣的。

The directory service cannot perform the requested operation on the RDN attribute of an object.

但是我前面提到的,我不知道還有有關Active Directory,我困惑如何處理它。

請有人幫助我。代碼如下。

void CreateADUser(string ppk,string pk,DataRow row) 
{ 
    string pass = GetPass(pk,row,LogSections.AD); 
    DirectoryEntry addept = adm.FindOU(ppk); 
    string sOU = adm.GetPrincipalPath(addept); 
    var aduser = adm.CreateNewUser(sOU, pk, pass, pk, null, null, adm.sDomain); 
    SetAdUserProperties(pk, pass, row); 
    MoveUser(ppk,pk); 
} 



void SetAdUserProperties(string pk,string pass,DataRow row) 
{ 
    if (row == null) return; 
    List<ADMapping> MappingPatterns = GetAdMappings(Words.User,false); 
    var colnames = Tool.GetColNames(row); 
    var aduser = adm.GetUser(pk); 
    var de=aduser.GetUnderlyingObject() as DirectoryEntry; 
    foreach (var ADMap in MappingPatterns) 
    { 
     string val = ADMap.Mapping; 
     val=Util.ReplaceColPattern(val, row); 
     SetProperty(de, ADMap.CN, val); 
    } 
    if (!string.IsNullOrWhiteSpace(pass)) 
    { 
     var UserPkColumn = AppConfigHelper.GetAppString(Words.SourceUserPKColumn); 
     UserPkColumn = Util.GetActualColName(UserPkColumn); 
     aduser.SetPassword(pass); 
     QueryHelper.Update(QueryHelper.ConnectionString, Words.ShadowUserTable 
        ,new SqlParameter[] { new SqlParameter("@passwd", pass) } 
        , new SqlParameter("@"+UserPkColumn,pk)); 
    } 

    aduser.Save(); 
} 

public void SetProperty(DirectoryEntry oDE, string sPropertyName, object sPropertyValue) 
{ 
    if (sPropertyValue != null && !string.IsNullOrWhiteSpace(sPropertyValue.ToString())) 
    { 
     if (oDE.Properties.Contains(sPropertyName)) 
     { 
      oDE.Properties[sPropertyName].Value = sPropertyValue; 
     } 
     else 
     { 
      oDE.Properties[sPropertyName].Add(sPropertyValue); 
     } 
     try 
     { 
      oDE.CommitChanges(); //exception here. 
      oDE.Close(); 
     } 
     catch (Exception) 
     { 

     } 

    } 


    } 

回答

0

嘗試做重命名前提交修改希望這將有助於

oDE.Rename("name=$name"); 
    oDE.CommitChanges(); 
+0

對不起我真的來不及看到你的答案。我添加了你的代碼,但它仍然拋出異常... –