调用commitChanges()在Active Directory中什么都不做?

时间:2010-01-26 17:44:24

标签: c# active-directory directoryservices

尽管我使用了CommitChanges函数,但似乎更改并未保存在ActiveDirectory中。我使用正确的方法来解决这个问题吗?

//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 个答案:

答案 0 :(得分:2)

好吧,你抓住底层对象,修改它,然后再次抓住它并在其上调用CommitChanges()....我认为这不会这样。

试试这个:

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();        
 }

这会改变什么吗?它现在有效吗?