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

我们刚刚为用户提供了一个新的用户创建应用程序.但是,这些用户需要能够通过应用程序创建用户,即使他们自己没有创建用户的权限.

在C#中,您如何模拟其他用户才能拥有此功能.此应用程序主要使用System.DirectoryServices.

代码段:

DirectoryEntry dEntry = new DirectoryEntry("LDAP://OU=");
DirectorySearcher dSearcher = new DirectorySearcher(dEntry);
//filter just user objects
dSearcher.SearchScope = SearchScope.Subtree;
dSearcher.Filter = "(&(objectClass=user)(mail=" + excel_Holding_Table.Rows[i]["EmailAddress"].ToString() + "))";
dSearcher.PageSize = 1000;
sResults = dSearcher.FindAll();

解决方法

您可以直接使用 DirectoryEntry类并指定用户名和密码:
DirectoryEntry de = new DirectoryEntry(path);

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

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

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

完整的代码示例可在以下位置获得:

Impersonation and DirectoryEntry

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么