使用 PrincipalContext 连接到活动目录
Connect to active directory with PrincipalContext
我编写的应用程序根据名称在活动目录中查找用户
当我尝试使用 ContextType.Domain 和域名作为字符串创建新的 PrincipalContext 时,出现异常 "The server could not be contacted."
所以为了获得我这样做的广告......
通过依次单击“开始”按钮 “开始”按钮的图片 、“控制面板”、“系统和维护”和“系统”来打开“系统”。
我从 http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on
得到的
这给了我
Something.Something(不是这个,而是两个字符串之间有一个 .)
所以当我 运行 以下代码并输入 Something.Something 作为域时,我在 new PrincipalContext(ContextType.Domain, domain) 上得到异常 "The server could not be contacted.";
我试过更改字符串的大小写,但似乎没有任何运气。
那么我应该为域使用什么?
public static void Main(string[] args)
{
try
{
Console.WriteLine("Enter Domain, then press enter.");
string domain = Console.ReadLine();
Console.WriteLine("Enter First Name, then press enter.");
string userName = Console.ReadLine();
//This is the line that always crashes throws error
var principalContext = new PrincipalContext(ContextType.Domain, domain);
var user = UserPrincipal.FindByIdentity(principalContext, userName);
if (user == null)
{
Console.WriteLine("User Not Found");
}
var groups = user.GetGroups(principalContext);
var result = new List<string>();
groups.ToList().ForEach(sr => result.Add(sr.SamAccountName));
foreach (var item in result)
{
Console.WriteLine(item);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Ashley 是正确的...假设您是 运行 加入域的计算机上的应用程序。
using System;
using System.DirectoryServices.AccountManagement;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter First Name, then press enter.");
var userName = Console.ReadLine();
// Will search the domain the application is running on
var principalContext = new PrincipalContext(ContextType.Domain);
var user = UserPrincipal.FindByIdentity(principalContext, userName);
if (user == null)
{
Console.WriteLine("User Not Found");
}
else
{
// Gets a list of the user's groups
var groups = user.GetGroups().ToList();
// Loops the groups and prints the SamAccountName
groups.ForEach(g => Console.WriteLine(g.SamAccountName));
}
Console.ReadKey();
}
}
}
如果您有几秒钟的时间等待您的数据形成一个大型 AD,那么请继续使用 PrincipalContext,但如果您想要以毫秒为单位的响应,请使用 DirectoryEntry、DirectorySearcher 和 .PropertiesToLoad。
下面是获取用户组的示例:
我编写的应用程序根据名称在活动目录中查找用户
当我尝试使用 ContextType.Domain 和域名作为字符串创建新的 PrincipalContext 时,出现异常 "The server could not be contacted."
所以为了获得我这样做的广告...... 通过依次单击“开始”按钮 “开始”按钮的图片 、“控制面板”、“系统和维护”和“系统”来打开“系统”。 我从 http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on
得到的这给了我 Something.Something(不是这个,而是两个字符串之间有一个 .)
所以当我 运行 以下代码并输入 Something.Something 作为域时,我在 new PrincipalContext(ContextType.Domain, domain) 上得到异常 "The server could not be contacted."; 我试过更改字符串的大小写,但似乎没有任何运气。
那么我应该为域使用什么?
public static void Main(string[] args)
{
try
{
Console.WriteLine("Enter Domain, then press enter.");
string domain = Console.ReadLine();
Console.WriteLine("Enter First Name, then press enter.");
string userName = Console.ReadLine();
//This is the line that always crashes throws error
var principalContext = new PrincipalContext(ContextType.Domain, domain);
var user = UserPrincipal.FindByIdentity(principalContext, userName);
if (user == null)
{
Console.WriteLine("User Not Found");
}
var groups = user.GetGroups(principalContext);
var result = new List<string>();
groups.ToList().ForEach(sr => result.Add(sr.SamAccountName));
foreach (var item in result)
{
Console.WriteLine(item);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Ashley 是正确的...假设您是 运行 加入域的计算机上的应用程序。
using System;
using System.DirectoryServices.AccountManagement;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter First Name, then press enter.");
var userName = Console.ReadLine();
// Will search the domain the application is running on
var principalContext = new PrincipalContext(ContextType.Domain);
var user = UserPrincipal.FindByIdentity(principalContext, userName);
if (user == null)
{
Console.WriteLine("User Not Found");
}
else
{
// Gets a list of the user's groups
var groups = user.GetGroups().ToList();
// Loops the groups and prints the SamAccountName
groups.ForEach(g => Console.WriteLine(g.SamAccountName));
}
Console.ReadKey();
}
}
}
如果您有几秒钟的时间等待您的数据形成一个大型 AD,那么请继续使用 PrincipalContext,但如果您想要以毫秒为单位的响应,请使用 DirectoryEntry、DirectorySearcher 和 .PropertiesToLoad。
下面是获取用户组的示例: