我在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验证用户名和密码?

有帮助吗?

解决方案

如果您使用的是.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();

其他提示

我发现在多个网站上的互联网上存在相同的代码,但这对我不起作用。 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。请注意我是如何使用“按位OR”设置安全(Kerberos)身份验证和SSL的。 ('|')运算符介于两个参数之间。

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

如果抛出异常,则凭据(或设置)很糟糕。如果没有例外,您将通过身份验证。异常消息通常表明出了什么问题。

此代码与您已有的代码非常相似,但在您拥有“路径”的地方使用域名,并且用户名不会与域名结合使用。一定要正确设置AuthenticationTypes。这可以决定身份验证的能力。

无论如何我想出来如果你使用vista上的用户名传递域名,它就不像“域\用户”那样工作了所以只是传递“用户”相反似乎工作正常 - 除非你必须在同一个域

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

不确定为什么它会需要提升的权限,但值得一试。

scroll top