【问题标题】:C# Get Domain Active directory Information by Windows CredentialsC# 通过 Windows 凭据获取域活动目录信息
【发布时间】:2014-11-11 18:32:35
【问题描述】:

我一直在网上搜索 Active Directory 和 Windows 身份验证。我已成功从域 AD 获取用户信息,但我必须传递用户名和密码。所以把你放到我的上下文中:

我有一个域,我在其中设置了我的用户。每个用户都将使用他们给定的凭据连接到域。因此,他们将登录到他们的 PC,当他们打开 VS 2013 C# 应用程序时,它将检查用户是否存在于域中,如果存在则返回 AD 信息,如果用户不存在,则显示登录页面以输入凭据.因为我可以让外部用户连接到我的域等...

现在我无法使用用户的 Windows 身份验证访问 AD,它在 Search.FindOne(); 上给我一个未知错误

public static void GetActiveDirectoryUser(string UserName) 
    {
        try
        {
            // Create LDAP connetion object
            DirectoryEntry ldapConnection = CreateDirectoryEntry();

            // Create Search object which operates on LDAP connection object
            // and set search object to only find the user specified
            DirectorySearcher search = new DirectorySearcher(ldapConnection);

            // Create results objects from search object
            SearchResult result = search.FindOne();

            if (result != null)
            {
                // User exists, cycle through LDAP fields (cn, telephonenumber, etc.)
                ResultPropertyCollection fields = result.Properties;

                foreach (string ldapField in fields.PropertyNames)
                {
                    // Cycle through objects in each field e.g group membership
                    foreach (Object objCollection in fields[ldapField])
                    {
                        Console.WriteLine(String.Format("{0, -20} : {1}", ldapField, objCollection.ToString()));
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception Caught:\n\n" + e.ToString());
        }
    }

    static DirectoryEntry CreateDirectoryEntry()
    {
        string pathDomainName = "WinNT://MyDomain/Fred,Person";

        DirectoryEntry ldapConnection = new DirectoryEntry(pathDomainName);

        return ldapConnection;
    }

这是我遇到的错误

System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()

但是当我使用这个字符串时

string pathDomainName = "LDAP://MyDomain";
DirectoryEntry directoryEntry = new DirectoryEntry(pathDomainName, "Fred", "f12345!");

它有效,它为我返回了用户的所有 AD,但我已经使用 windows 身份验证登录,为什么我还要再次传递凭据?我只需要知道用户是否存在于它的域中

谢谢

【问题讨论】:

    标签: c# visual-studio-2013 active-directory windows-authentication


    【解决方案1】:

    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
        // find a user
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
    
        if(user != null)
        {
           // do something here....     
        }
    
        // or alternatively: get the currently logged in user
        UserPrincipal current = UserPrincipal.Current;
    
        .....
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多