【问题标题】:Active Directory Linqpad vs Visual StudioActive Directory Linqpad 与 Visual Studio
【发布时间】:2016-01-06 15:23:48
【问题描述】:

我需要针对 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");
}

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

【问题讨论】:

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


    【解决方案1】:

    尝试将您的代码更改为此我在本地、远程服务器上都尝试过,并且都可以工作

    如果用户为 null,则表示在 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多