【发布时间】:2016-08-05 02:17:44
【问题描述】:
我写了一个应用程序,它根据那里的名称在活动目录中查找用户
当我尝试使用 ContextType.Domain 和作为字符串的域名创建新的 PrincipalContext 时,我收到异常“无法联系服务器。”
所以为了得到我这样做的广告...... 通过单击开始按钮 打开系统 开始按钮的图片,单击控制面板,单击系统和维护,然后单击系统。 我从http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on得到的
这给了我 Something.Something(不是这个,而是两个带有 . 的字符串)
因此,当我运行以下代码并输入Something.Something 作为域时,我收到异常“无法联系服务器”。在新的 PrincipalContext(ContextType.Domain, domain) 上; 我试过改变字符串的大小写,但似乎没有任何运气。
那么我应该为域使用什么?
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);
}
}
【问题讨论】:
-
如果你的机器已经加入域,你应该可以使用
var principalContext = new PrincipalContext(ContextType.Domain);。
标签: c# active-directory connection-string