调用 commitChanges() 在 Active Directory 中没有任何作用?

发布于 2024-08-19 22:27:36 字数 1753 浏览 9 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2024-08-26 22:27:36

好吧,您正在抓取底层对象,修改它,然后再次抓取它并对其调用 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();        
 }

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

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??

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文