【发布时间】:2014-01-08 16:23:11
【问题描述】:
我有以下代码可以连接到我们的广告:-
List<DomainContext> results = new List<DomainContext>();
string ADServerName = System.Web.Configuration.WebConfigurationManager.AppSettings["ADServerName"];
using (var context = new PrincipalContext(ContextType.Domain, ADServerName))
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
以上在我们的开发机器上工作,因为 AD 和 asp.net mvc 在同一台机器上。目前我需要连接到我们在不同机器上的登台广告。所以我需要将 AD 服务帐户(用户名和密码)传递给PrincipalContext。任何人都可以建议吗?我在网上看到我需要使用UserPrincipal如下:-
public UserPrincipal(
PrincipalContext context,
string samAccountName,
string password,
bool enabled
)
但我不确定如何相应地修改我的代码?
谢谢
编辑 我尝试这样做以仅显示在其杰出帐户中具有 brancjA 或 brachB 的用户:-
public List<DomainContext> GetADUsers(string term=null)
{
string[] types = new string[] { "BranchA", "BranchB" };
List<DomainContext> results = new List<DomainContext>();
string ADServerName = System.Web.Configuration.WebConfigurationManager.AppSettings["ADServerName"];
using (var context = new PrincipalContext(ContextType.Domain, ADServerName, "username", "password"))
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
var searchResults = searcher.FindAll();
foreach (Principal p in searchResults)
{
if ((term == null || p.SamAccountName.ToString().ToUpper().StartsWith(term.ToUpper())) && (types.Contains(p.DistinguishedName)))
但结果是没有用户返回。你能建议吗?
【问题讨论】:
标签: .net asp.net-mvc asp.net-mvc-4 active-directory