34

我在 Windows Vista Ultimate SP1 上使用以下代码来查询我们的活动目录服务器,以检查域中用户的用户名和密码。

public Object IsAuthenticated()
{
    String domainAndUsername = strDomain + "\\" + strUser;
    DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, strPass);
    SearchResult result;
    try
    {
        //Bind to the native AdsObject to force authentication.         

        DirectorySearcher search = new DirectorySearcher(entry) { Filter = ("(SAMAccountName=" + strUser + ")") };

        search.PropertiesToLoad.Add("givenName"); // First Name                
        search.PropertiesToLoad.Add("sn"); // Last Name
        search.PropertiesToLoad.Add("cn"); // Last Name

        result = search.FindOne();

        if (null == result)
        {
            return null;
        }

        //Update the new path to the user in the directory.
        _path = result.Path;
        _filterAttribute = (String)result.Properties["cn"][0];
    }
    catch (Exception ex)
    {
        return new Exception("Error authenticating user. " + ex.Message);
    }
    return user;
}

目标使用 .NET 3.5,并使用 VS 2008 标准编译

我在运行应用程序的域管理员的域帐户下登录。

该代码在 Windows XP 上完美运行;但在 Vista 上运行时出现以下异常:

System.DirectoryServices.DirectoryServicesCOMException (0x8007052E): Logon failure: unknown user name or bad password.

   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()
   at Chain_Of_Custody.Classes.Authentication.LdapAuthentication.IsAuthenticated()
   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()
   at Chain_Of_Custody.Classes.Authentication.LdapAuthentication.IsAuthenticated()

我尝试更改身份验证类型,但不确定发生了什么。


另见针对 Active Directory 验证用户名和密码?

4

4 回答 4

49

如果您使用的是 .net 3.5,请改用此代码。

要验证用户:

PrincipalContext adContext = new PrincipalContext(ContextType.Domain);

using (adContext)
{
     return adContext.ValidateCredentials(UserName, Password);
}

如果您需要查找用户对对象的 R/W 属性,请执行以下操作:

PrincipalContext context = new PrincipalContext(ContextType.Domain);
UserPrincipal foundUser = 
    UserPrincipal.FindByIdentity(context, "jdoe");

这是使用 System.DirectoryServices.AccountManagement 命名空间,因此您需要将其添加到您的 using 语句中。

如果您需要将 UserPrincipal 对象转换为 DirectoryEntry 对象以使用旧代码,您可以执行以下操作:

DirectoryEntry userDE = (DirectoryEntry)foundUser.GetUnderlyingObject();
于 2009-01-01T18:20:35.537 回答
8

我发现相同的代码在 Internet 上的多个网站上流传,但它对我不起作用。Steve Evans 可能是对的,如果您使用的是 .NET 3.5,则不应使用此代码。但是,如果您仍在使用 .NET 2.0,您可以尝试通过以下方式对您的 AD 服务进行身份验证:

DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, 
   userName, password, 
   AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer);
object nativeObject = entry.NativeObject;

第一行使用域、用户名和密码创建一个 DirectoryEntry 对象。它还设置 AuthenticationTypes。请注意我是如何在两个参数之间使用“按位或”('|')运算符设置安全(Kerberos)身份验证和 SSL 的。

第二行使用第一行的信息强制“入口”的 NativeObject 绑定到 AD 服务。

如果抛出异常,则凭据(或设置)不正确。如果没有例外,则您已通过身份验证。异常消息通常会指示出了什么问题。

此代码与您已经拥有的代码非常相似,但是在您拥有“路径”的地方使用域,并且用户名没有与域结合。确保也正确设置您的 AuthenticationTypes。这可以成就或破坏身份验证的能力。

于 2010-03-26T19:01:02.433 回答
1

无论如何我都想通了

于 2008-12-31T14:50:02.293 回答
0

绑定到 LDAP 是否需要提升权限 (UAC)?您可以尝试以管理员身份运行 Visual Studio 和/或应用程序,看看是否有帮助。如果这是问题所在,您可以随时向应用程序添加清单并将其设置为需要提升,这样当用户运行它时它会提示。

不知道为什么它需要更高的权限,但值得一试。

于 2008-12-30T17:26:13.417 回答