且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# 使用不同的用户凭据访问活动目录

更新时间:2023-01-04 16:29:38

您可以使用 DirectoryEntry 类直接指定用户名和密码:

You can use the DirectoryEntry class directly and specify the username and password:

DirectoryEntry de = new DirectoryEntry(path);

de.Username = "username";
de.Password = "password";

并从 de 对象访问 Active Directory.或者您可以使用 WindowsIdentity 类并模拟用户:

And access Active Directory from the de object. Or you can use the WindowsIdentity class and and impersonate a User:

WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

完整代码示例位于:

模拟和目录条目