3

尽管我使用了 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();
4

1 回答 1

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

这有什么改变吗?现在可以用了吗??

于 2010-01-26T18:07:54.510 回答