简体   繁体   English

使用 PrincipalContext 连接到活动目录

[英]Connect to active directory with PrincipalContext

Im writtem app that looks for a user in active directory based on there name我是根据名称在活动目录中查找用户的书面应用程序

When I try to create new PrincipalContext with ContextType.Domain and the domain name as a string, I get the exception "The server could not be contacted."当我尝试使用 ContextType.Domain 和域名作为字符串创建新的 PrincipalContext 时,出现异常“无法联系服务器”。

So to get the the AD that I did this ... Open System by clicking the Start button Picture of the Start button, clicking Control Panel, clicking System and Maintenance, and then clicking System.因此,为了获得我执行的 AD... 打开系统,方法是单击“开始”按钮“开始”按钮的图片、“控制面板”、“系统和维护”,然后单击“系统”。 Which I got from http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on我从http://windows.microsoft.com/en-gb/windows-vista/find-the-domain-your-computer-is-on得到的

Which gave me Something.Something (not this, but two strings with a . between them)这给了我Something.Something(不是这个,而是两个字符串,它们之间有一个 . )

So when I run the following code and enter Something.Something as the domain, I get the exception "The server could not be contacted."因此,当我运行以下代码并输入Something.Something 作为域时,我收到异常“无法联系服务器”。 on the new PrincipalContext(ContextType.Domain, domain);在新的 PrincipalContext(ContextType.Domain, domain) 上; I have tried changing the case of the string and the but dont seem to have any luck.我曾尝试更改字符串的大小写,但似乎没有任何运气。

So what should I be using for the 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);
        }
    }

Ashley is correct... assuming you are running the application on a machine that is joined to the domain. 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();
        }
    }
}

If you've got several seconds to spare waiting for your data form a large AD, then go ahead and use PrincipalContext but if you want your response in milliseconds, use DirectoryEntry, DirectorySearcher and .PropertiesToLoad.如果您有几秒钟的时间等待来自大型 AD 的数据,请继续使用 PrincipalContext,但如果您希望以毫秒为单位进行响应,请使用 DirectoryEntry、DirectorySearcher 和 .PropertiesToLoad。

Here's an example for getting the groups for a user:以下是获取用户组的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于在Active Directory中查询的PrincipalContext - PrincipalContext for query in Active Directory C#Active Directory主体上下文权限 - C# Active Directory Principalcontext Permissions Active Directory PrincipalContext.ValidateCredentials域消除歧义 - Active Directory PrincipalContext.ValidateCredentials domain disambiguation 从Active Directory PrincipalContext获取所有用户 - Getting all users from Active Directory PrincipalContext 在 Active Directory C# 中创建用户的 PrincipalContext 或 DirectoryEntry 哪个更好 - Which is better PrincipalContext or DirectoryEntry for user creation in Active Directory C# Active Directory 跨域 - 使用 PrincipalContext 的组成员 - Active directory cross domain - group members using PrincipalContext 需要在PrincipalContext中的Active Directory中重置密码并使密码过期 - Need to reset and expire a password in Active Directory within a PrincipalContext 为什么不能通过PrincipalContext联系Active Directory服务器? - Why can't the Active Directory server be contacted via PrincipalContext? 如果计算机未加入域,是否可以使用PrincipalContext获取Active Directory信息 - Can PrincipalContext be used to get Active Directory information if computer is not domained joined C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf 错误 - C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM