Active Directory Linqpad与Visual Studio

时间:2016-01-06 15:23:49

标签: c# visual-studio active-directory linqpad

我需要针对Active Directory对用户进行身份验证。相同的代码在Visual Studio和LinqPad中运行。 LinqPad工作正常但是在Visual Studio中,它出现了一个非常有用的未知错误,堆栈跟踪如下:

System.DirectoryServices.Protocols.LdapException: A local error occurred.    
     at System.DirectoryServices.Protocols.LdapConnection.BindHelper(NetworkCredential newCredential, Boolean needSetCredential)    
     at System.DirectoryServices.AccountManagement.CredentialValidator.lockedLdapBind(LdapConnection current, NetworkCredential creds, ContextOptions contextOptions)    
     at System.DirectoryServices.AccountManagement.CredentialValidator.BindLdap(NetworkCredential creds, ContextOptions contextOptions)    
     at System.DirectoryServices.AccountManagement.CredentialValidator.Validate(String userName, String password)    
     at System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials(String userName, String password)

两者中的代码仅在LinqPad中运行:

using (System.DirectoryServices.AccountManagement.PrincipalContext pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain))
{
   return pc.ValidateCredentials("SomeName", SomePassword");
}

我还使用/不使用Admin在VS之外运行了exe,并且还使用了cmd行的runas。 如果有人知道为什么这些行为会有所不同,那么我们将非常感激。

1 个答案:

答案 0 :(得分:1)

尝试将代码更改为此我在本地远程服务器上尝试了这一点,并且都在工作

  

如果用户为空,则表示在AD

中找不到它们
string fullName = null;
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context,"your domain username goes here"))
    {
        if (user != null)
        {
            fullName = user.DisplayName;
        }
    }
}