Im应用程序,它根据active目录中的名称查找用户
当我尝试用PrincipalContext和域名作为字符串创建新的ContextType.Domain时,我会得到“无法联系到服务器”的异常。
所以为了拿到广告我做了这个..。单击“开始”按钮的“开始”按钮图片,单击“控制面板”,单击“系统和维护”,然后单击“系统”。我从http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on那里得到的
这给了我Something.Something (不是这个,而是两个带a的字符串。(他们之间)
因此,当我运行以下代码并输入Something.Something作为域时,我会得到“无法联系到服务器”的异常。在新的PrincipalContext(ContextType.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);
}
}发布于 2016-04-13 22:27:44
艾希礼是对的。假设您正在连接到域的计算机上运行应用程序。
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();
}
}
}发布于 2021-02-01 03:36:38
如果您有几秒钟的时间等待数据形成一个大AD,那么继续使用PrincipalContext,但是如果您想要在毫秒内响应,请使用DirectoryEntry、DirectorySearcher和.PropertiesToLoad。
下面是一个为用户获取组的示例:
https://stackoverflow.com/questions/36607658
复制相似问题