【问题标题】:Connect to active directory with PrincipalContext使用 PrincipalContext 连接到活动目录
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 干杯,我试过了,得到了异常“无法联系服务器。”在新的 PrincipalContext(ContextType.Domain) 行上。这就是让我尝试添加域的原因
  • 我在没有域的机器上尝试了我的代码并得到了同样的错误。尝试运行它来看看你得到了什么。应该类似于 {domain}\\{userName}。 var name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
【解决方案2】:

如果您有几秒钟的时间等待来自大型 AD 的数据,请继续使用 PrincipalContext,但如果您希望在毫秒内得到响应,请使用 DirectoryEntry、DirectorySearcher 和 .PropertiesToLoad。

以下是获取用户组的示例:

https://stackoverflow.com/a/65986796/5248400

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2016-11-18
    • 2019-10-28
    • 1970-01-01
    • 2011-11-26
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多