This page has moved. You will be automatically redirected to its new location in 10 seconds. If you aren't forwarded to the new page, click here.

Search This Blog

Monday, September 28, 2009

System.DirectoryServices.DirectoryServicesCOMException: Logon failure: unknown user name or bad password.

http://stackoverflow.com/questions/400872/c-active-directory-check-username-password

Resolved my issue by using "Imports System.DirectoryServices.AccountManagement"
Project was working well in XP but not in Windows 7

Replaced:

Dim domainEntry As DirectoryServices.DirectoryEntry
Dim searcher As DirectoryServices.DirectorySearcher
Dim results As DirectoryServices.SearchResultCollection
Dim domainLogin As String
Dim fullName As String

domainLogin = User.Identity.Name

'Strip off domain prefix
domainLogin = domainLogin.Substring(domainLogin.IndexOf("\") + 1, domainLogin.Length - domainLogin.IndexOf("\") - 1)

domainEntry = New DirectoryServices.DirectoryEntry("LDAP://domainname.com", "domainname\username", "Password")
searcher = New DirectoryServices.DirectorySearcher(domainEntry, "(SAMAccountname=" + domainLogin + ")")
results = searcher.FindAll()


with:

Dim domainEntry As DirectoryServices.DirectoryEntry
Dim searcher As DirectoryServices.DirectorySearcher
Dim results As DirectoryServices.SearchResultCollection
Dim domainLogin As String
Dim fullName As String

domainLogin = User.Identity.Name

'Strip off domain prefix
domainLogin = domainLogin.Substring(domainLogin.IndexOf("\") + 1, domainLogin.Length - domainLogin.IndexOf("\") - 1)
Dim context As PrincipalContext = New PrincipalContext(ContextType.Domain)
Dim foundUser As UserPrincipal = UserPrincipal.FindByIdentity(context, "username")
domainEntry = CType(foundUser.GetUnderlyingObject(), DirectoryEntry)

searcher = New DirectoryServices.DirectorySearcher(domainEntry, "(SAMAccountname=" + domainLogin + ")")
results = searcher.FindAll()

No comments:

Post a Comment