Asp.net mvc 4 将posixAccount写入LDAP不会';行不通

Asp.net mvc 4 将posixAccount写入LDAP不会';行不通,asp.net-mvc-4,ldap,Asp.net Mvc 4,Ldap,我尝试将LDAP上的PosixAccount写入现有用户。我没有收到任何错误,但在检查LDAP时,新条目尚未写入 我先添加了一个新用户,效果很好! => 之后,我想为该用户编写posixAccount,但它不起作用 也许有人能帮我检查一下我做错了什么 => 我认为您得到的错误将为进一步诊断提供信息 当你在AD中创建一个对象时,我很确定即使你没有指定CN,你也会得到一个默认的命名属性CN set。因此,此posixAccount创建(正在设置cn)可能与现有cn值冲突。我忘了CN在AD中是多值的还

我尝试将LDAP上的PosixAccount写入现有用户。我没有收到任何错误,但在检查LDAP时,新条目尚未写入

我先添加了一个新用户,效果很好! =>

之后,我想为该用户编写posixAccount,但它不起作用 也许有人能帮我检查一下我做错了什么

=>


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


当你在AD中创建一个对象时,我很确定即使你没有指定CN,你也会得到一个默认的命名属性CN set。因此,此posixAccount创建(正在设置cn)可能与现有cn值冲突。我忘了CN在AD中是多值的还是单值的,但如果它是单值的,则更有意义。

我认为您得到的错误将为进一步诊断提供信息。调用后的调用成功=>de.CommitChanges();我没有发现任何错误,这让我很困惑。嗯,你的确切意思是cn有什么问题?(是否应该是我之前编写的现有用户?@user2169239在AD中创建对象时,通过LDAP浏览器查看它。它已经有CN属性了吗?我猜是的。现在,您尝试添加需要CN的posixAccount,因此再次添加它。如果AD的CN是单值的,那么添加第二个CN属性值就是非法操作。这就是我要说的。是的,已经有了cn属性。单值设置为“否”。
        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;
    }
     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;
    }