2013-03-14 65 views
1

我試圖將LDAP上的PosixAccount寫入現有用戶。我沒有得到任何錯誤,但是在檢查LDAP時新條目還沒有寫入。將posixAccount寫入LDAP不起作用

我添加了一個新的用戶第一個工作得很好! =>

 public bool RegisterUser(UserObject userObj, HttpContext httpContext){ 
     bool success = false; 

     //create a directory entry 
     using (DirectoryEntry de = new DirectoryEntry()) 
     { 
      try 
      { 
       InitializeCommonDataForDirectoryEntry(
        de, 
        String.Format("{0}/{1}", 
         GetConfigEntry(Common.CommonDefinitions.CE_LDAP_CONFIG_SERVER, httpContext), 
         GetConfigEntry(Common.CommonDefinitions.CE_LDAP_CONFIG_DIRECTORY_ENTRY_ROOT, httpContext)), 
         httpContext); 

       DirectorySearcher ds = new DirectorySearcher(de); 
       ds.SearchScope = System.DirectoryServices.SearchScope.Subtree; 
       ds.Filter = "(&(objectClass=organizationalUnit)(ou=people))"; 

       SearchResult result = ds.FindOne(); 
       if (result != null) 
       { 
        DirectoryEntry myDirectoryEntry = result.GetDirectoryEntry(); 
        DirectoryEntry newEntry = myDirectoryEntry.Children.Add(String.Format("cn={0}", userObj.userName), "inetOrgPerson"); 

        if (userObj.company != null && !userObj.company.Equals(String.Empty)) 
         newEntry.Properties["businessCategory"].Add(String.Format("{0}", userObj.company)); 
        newEntry.Properties["givenName"].Add(String.Format("{0}", userObj.firstName)); 
        newEntry.Properties["sn"].Add(String.Format("{0}", userObj.lastName)); 
        newEntry.Properties["uid"].Add(String.Format("{0}", userObj.userName)); 
        newEntry.Properties["mail"].Add(String.Format("{0}", userObj.email)); 
        userObj.password = GenerateSaltedSHA1(userObj.password); 
        newEntry.Properties["userPassword"].Add(String.Format("{0}", userObj.password)); 
        newEntry.Properties["pager"].Add(String.Format("{0}", userObj.newsletter)); 
        newEntry.Properties["initials"].Add(String.Format("{0}", GetConfigEntry(Common.CommonDefinitions.CE_MOWEE_PACKAGE_1, httpContext))); 

        newEntry.CommitChanges(); 
        newEntry.RefreshCache(); 
        success = true; 
       } 
      } 
      catch (Exception ex) 
      { 
       Trace.Write("Exception : RegisterUser: " + ex); 
       GeneralUtils.SendBugMail(ex, httpContext); 
      } 
     } 
     return success; 
    } 

後,我想寫的posixAccount該用戶,這是不工作 也許有人可以幫我請,檢查我做錯了什麼!?

=>

 public bool WritePosixAccountDataForRegisteredUser(UserObject userObj, HttpContext httpContext) 
    { 
     bool success = false; 

     //create a directory entry 
     using (DirectoryEntry de = new DirectoryEntry()) 
     { 
      try 
      { 
       InitializeCommonDataForDirectoryEntry(
        de, 
        String.Format("{0}/ou=people,{1}", 
         GetConfigEntry(Common.CommonDefinitions.CE_LDAP_CONFIG_SERVER, httpContext), 
         GetConfigEntry(Common.CommonDefinitions.CE_LDAP_CONFIG_DIRECTORY_ENTRY_ROOT, httpContext)), 
         httpContext); 

       DirectorySearcher ds = new DirectorySearcher(de); 
       ds.SearchScope = System.DirectoryServices.SearchScope.Subtree; 
       ds.Filter = String.Format("(&(objectClass=*)(cn={0}))", userObj.userName); 

       SearchResult result = ds.FindOne(); 
       if (result != null) 
       { 
        DirectoryEntry userEntry = result.GetDirectoryEntry(); 

        //mandatory attributes 
        /* 
        *  cn 
          gidNumber 
          homeDirectory 
          uid 
          uidNumber 
        * */ 

        IADsPropertyList propList = (IADsPropertyList)userEntry.NativeObject; 

        ActiveDs.PropertyEntry myNewEntry1 = new ActiveDs.PropertyEntry(); 
        ActiveDs.IADsPropertyValue propVal1 = new ActiveDs.PropertyValue(); 
        propVal1.CaseIgnoreString = "posixAccount"; 
        propVal1.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        myNewEntry1.Name = "objectClass"; 
        myNewEntry1.Values = new object[] { propVal1 }; 
        myNewEntry1.ControlCode = (int)ADS_PROPERTY_OPERATION_ENUM.ADS_PROPERTY_APPEND; 
        myNewEntry1.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        propList.PutPropertyItem(myNewEntry1); 

        ActiveDs.PropertyEntry myNewEntry2 = new ActiveDs.PropertyEntry(); 
        ActiveDs.IADsPropertyValue propVal2 = new ActiveDs.PropertyValue(); 
        propVal2.CaseIgnoreString = "504"; 
        propVal2.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        myNewEntry2.Name = "gidNumber"; 
        myNewEntry2.Values = new object[] { propVal2 }; 
        myNewEntry2.ControlCode = (int)ADS_PROPERTY_OPERATION_ENUM.ADS_PROPERTY_APPEND; 
        myNewEntry2.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        propList.PutPropertyItem(myNewEntry2); 

        ActiveDs.PropertyEntry myNewEntry3 = new ActiveDs.PropertyEntry(); 
        ActiveDs.IADsPropertyValue propVal3 = new ActiveDs.PropertyValue(); 
        propVal3.CaseIgnoreString = "/data/WowzaMediaServer-3.0.3/content/mowee/" + userObj.userName; 
        propVal3.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        myNewEntry3.Name = "homeDirectory"; 
        myNewEntry3.Values = new object[] { propVal3 }; 
        myNewEntry3.ControlCode = (int)ADS_PROPERTY_OPERATION_ENUM.ADS_PROPERTY_APPEND; 
        myNewEntry3.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        propList.PutPropertyItem(myNewEntry3); 

        ActiveDs.PropertyEntry myNewEntry4 = new ActiveDs.PropertyEntry(); 
        ActiveDs.IADsPropertyValue propVal4 = new ActiveDs.PropertyValue(); 
        propVal4.CaseIgnoreString = "1100"; 
        propVal4.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        myNewEntry4.Name = "uidNumber"; 
        myNewEntry4.Values = new object[] { propVal4 }; 
        myNewEntry4.ControlCode = (int)ADS_PROPERTY_OPERATION_ENUM.ADS_PROPERTY_APPEND; 
        myNewEntry4.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        propList.PutPropertyItem(myNewEntry4); 

        ActiveDs.PropertyEntry myNewEntry5 = new ActiveDs.PropertyEntry(); 
        ActiveDs.IADsPropertyValue propVal5 = new ActiveDs.PropertyValue(); 
        propVal5.CaseIgnoreString = userObj.userName; 
        propVal5.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        myNewEntry5.Name = "cn"; 
        myNewEntry5.Values = new object[] { propVal5 }; 
        myNewEntry5.ControlCode = (int)ADS_PROPERTY_OPERATION_ENUM.ADS_PROPERTY_APPEND; 
        myNewEntry5.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        propList.PutPropertyItem(myNewEntry5); 

        ActiveDs.PropertyEntry myNewEntry6 = new ActiveDs.PropertyEntry(); 
        ActiveDs.IADsPropertyValue propVal6 = new ActiveDs.PropertyValue(); 
        propVal6.CaseIgnoreString = userObj.userName; 
        propVal6.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        myNewEntry6.Name = "uid"; 
        myNewEntry6.Values = new object[] { propVal6 }; 
        myNewEntry6.ControlCode = (int)ADS_PROPERTY_OPERATION_ENUM.ADS_PROPERTY_APPEND; 
        myNewEntry6.ADsType = (int)ADSTYPEENUM.ADSTYPE_CASE_IGNORE_STRING; 
        propList.PutPropertyItem(myNewEntry6); 

        de.RefreshCache(new String[] { "objectClass" }); 
        de.RefreshCache(new String[] { "gidNumber" }); 
        de.RefreshCache(new String[] { "homeDirectory" }); 
        de.RefreshCache(new String[] { "uidNumber" }); 
        de.RefreshCache(new String[] { "cn" }); 
        de.RefreshCache(new String[] { "uid" }); 

        de.CommitChanges(); 
        success = true; 
       } 
      } 
      catch (Exception ex) 
      { 
       Trace.Write("Exception : RegisterUser: " + ex); 
       GeneralUtils.SendBugMail(ex, httpContext); 
      } 
     } 
     return success; 
    } 

回答

0

我認爲你的錯誤將信息進行進一步診斷任何。

當你在AD中創建一個對象時,我很確定,即使你沒有指定一個CN,你也會得到一個CN集的默認命名屬性。因此,此posixAccount創建(即設置cn)可能與現有cn值衝突。我忘記了CN是多值的還是單值的,但如果它是單值的,這會更有意義。

+0

我認爲你得到的錯誤將有助於進一步診斷。 呼叫成功後的呼叫=> de.CommitChanges(); 我沒有得到任何錯誤,這是令我困惑的。 嗯你到底是什麼意思是錯誤的cn? (應該是我之前編寫的現有用戶,還是不是?) – AndrewChurcher 2013-03-14 13:36:26

+0

@ user2169239當您在AD中創建對象時,請通過LDAP瀏覽器查看它。它是否有CN屬性?我的猜測是,它確實如此。現在,您嘗試添加需要CN的posixAccount,以便您再次添加它。如果AD的CN是單值的,那麼這是一個添加第二個CN屬性值的非法操作。這就是我要去的地方。 – geoffc 2013-03-14 17:50:31

+0

是的,已經有一個cn屬性。而單值被設置爲「否」。 – AndrewChurcher 2013-03-14 18:21:02