1

我尝试将 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;
    }
4

1 回答 1

0

我认为您得到的错误将为进一步诊断提供信息。

当您在 AD 中创建对象时,我很确定即使您没有指定 CN,您也会获得 CN 集的默认命名属性。所以这个设置 cn 的 posixAccount create 可能与现有的 cn 值冲突。我忘记了 CN 在 AD 中是多值还是单值,但如果它是单值,这将更有意义。

于 2013-03-14T12:24:29.640 回答