将posixAccount写入LDAP不起作用

时间:2013-03-14 10:19:59

标签: asp.net-mvc-4 ldap

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

1 个答案:

答案 0 :(得分:0)

我认为你得到的错误可以为进一步诊断提供信息。

在AD中创建对象时,我非常确定即使您没有指定CN,也会获得CN集的默认命名属性。因此,设置cn的posixAccount创建可能与现有的cn值冲突。我忘记了CN在AD中是多值还是单值,但如果它是单值的话,那就更有意义了。