Calling commitChanges() does nothing in Active Directory?

亡梦爱人 提交于 2019-12-10 18:52:00

问题


It seems that the changes are not saving within ActiveDirectory despite me using the CommitChanges function. Am I using the correct approach to solve this issue?

//Test OU Group: OU=First Group,OU=Domain Users,DC=DOMAIN,DC=com
String userName, password;

Console.Write("Username: ");
userName = Console.ReadLine();
Console.Write("Password: ");
password = Console.ReadLine();

//ENTER AD user account validation code here

String strLDAPpath = "LDAP://OU=First Group,OU=Domain Uers,DC=DOMAIN,DC=com";
DirectoryEntry entry = new DirectoryEntry(strLDAPpath,userName,password,AuthenticationTypes.Secure);
//DirectoryEntry entry = new DirectoryEntry(strLDAPpath);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(ObjectCategory=user)";
foreach (SearchResult result in mySearcher.FindAll())
{
    IADsTSUserEx entryX = (IADsTSUserEx)result.GetDirectoryEntry().NativeObject;
    int isTrue = 1;
    entryX.ConnectClientDrivesAtLogon = isTrue;
    entryX.ConnectClientPrintersAtLogon = isTrue;
    entryX.DefaultToMainPrinter = isTrue;
    result.GetDirectoryEntry().CommitChanges();        
}
Console.WriteLine("Changes have been made. Press Enter to continue...");
Console.ReadLine();
////entry = new DirectoryEntry(strLDAPpath, userName, password, AuthenticationTypes.Secure);
//mySearcher = new DirectorySearcher(entry);
//mySearcher.Filter = "(ObjectCategory=user)";
foreach(SearchResult result in mySearcher.FindAll())
{
    IADsTSUserEx entryX = (IADsTSUserEx)result.GetDirectoryEntry().NativeObject;
    Console.WriteLine(result.GetDirectoryEntry().Properties["name"].Value + "\t" + "Drives " + entryX.ConnectClientDrivesAtLogon + "\t" + "Printers " + entryX.ConnectClientPrintersAtLogon + "\t" + "Default " + entryX.DefaultToMainPrinter);
}
entry.Close();
Console.ReadLine();

回答1:


Well, you're grabbing the underlying object, modifying it, and then grabbing it one more time and calling CommitChanges() on it.... I think this won't work this way.

Try this:

mySearcher.Filter = "(ObjectCategory=user)";

foreach (SearchResult result in mySearcher.FindAll())
{
     DirectoryEntry resultEntry = result.GetDirectoryEntry();
     IADsTSUserEx entryX = (IADsTSUserEx)resultEntry.NativeObject;

     int isTrue = 1;
     entryX.ConnectClientDrivesAtLogon = isTrue;
     entryX.ConnectClientPrintersAtLogon = isTrue;
     entryX.DefaultToMainPrinter = isTrue;

     resultEntry.CommitChanges();        
 }

Does that change anything? Does it work now??



来源:https://stackoverflow.com/questions/2141372/calling-commitchanges-does-nothing-in-active-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!