【发布时间】:2010-09-28 21:20:55
【问题描述】:
我在 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()
我已尝试更改身份验证类型,但不确定发生了什么。
另见:Validate a username and password against Active Directory?
【问题讨论】:
-
这真的是一模一样的吗?此人收到异常消息,而不是询问如何操作...
-
由于您使用的是 .NET 3.5,因此您可以选择使用 System.DirectoryServices.AccountManagement。我没有在工作中使用 Vista,但由于它是为 3.5 设计的,它可能与 Vista 更兼容...
-
我不会把这叫做骗子......
-
无论如何我想通了在同一个域中
标签: c# windows-vista active-directory ldap