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

C# accessing active directory with different user credentials(C# 使用不同的用户凭据访问活动目录)
本文介绍了C# 使用不同的用户凭据访问活动目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

There is a new user creation application that we have just provided our users. However these users need the ability to creation users through the application even though they themselves do not have permission to create users.

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

In C# how do you impersonate another user in order to have this functionality. This application primary using 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 类直接指定用户名和密码:

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

完整代码示例位于:

模拟和目录条目

这篇关于C# 使用不同的用户凭据访问活动目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to determine the type (AD User vs. AD Group) of an account?(如何确定帐户的类型(AD 用户与 AD 组)?)
How to resolve quot;The server does not support the control. The control is critical.quot; Active Directory error(如何解决“服务器不支持控件.控制至关重要.活动目录错误)
How to authenticate users with a customer#39;s (remote) active directory server(如何使用客户的(远程)活动目录服务器对用户进行身份验证)
How to know if my DirectoryEntry is really connected to my LDAP directory?(如何知道我的 DirectoryEntry 是否真的连接到我的 LDAP 目录?)
Add member to AD group from a trusted domain(将成员从受信任的域添加到 AD 组)
How to retrieve Users in a Group, including primary group users(如何检索组中的用户,包括主要组用户)