LDAP Authentication with only User Name in Windows Application


Authenticate windows logged in user with LDAP Directory without passing password. It will use for some of the forms in application by passing only USername and authenticate it where password is not possible to get or don't want to provide Login screen.

Hi All,

Here i am explaining the Active Directory authentication. basically it's simple by passing User Name and Password to LDAP domain and check the data.

Here i am explaining the LDAP authentication without passing Password. By using windows logged in user name we can do the LDAP check whether user is active or not and prompt it same.



'use below line for reference.

Imports System.DirectoryServices

'below code is for get the Windows Logged in USer Name

Dim win As System.Security.Principal.WindowsIdentity
win = System.Security.Principal.WindowsIdentity.GetCurrent()
LoggedInUserName = win.Name.Substring(win.Name.IndexOf("\") + 1)


'below code is for check the user name in LDAP

'object for Directory Search
Dim objDirSearcher As DirectorySearcher = Nothing

'object for result of Search
Dim objResult As SearchResultCollection = Nothing

'LDAP server name which is defined in app.config file
Dim ldapServer As String = ConfigurationManager.AppSettings(clsConstant.LDAPServer)

'LDAP Domain name which is defined in app.config file
Dim ldapDomain As String = ConfigurationManager.AppSettings(clsConstant.LDAPDomain)

Check_If_Member_Of_AD_Group = False

'search the directory
objDirSearcher = New DirectorySearcher("LDAP://" & ldapServer)

'Filter Directory by passing above loggedInUSerName
objDirSearcher.Filter = String.Format("(&(objectClass=user)(sAMAccountName={0}))", LoggedInUserName)

'get the result
objResult = objDirSearcher.FindAll()

If (objResult.Count > 0) Then
Return True
else
Return False
End If


Comments

Author: venkii21 Jul 2011 Member Level: Silver   Points : 1

 

Imports System.DirectoryServices

'below code is for get the Windows Logged in USer Name

Dim win As System.Security.Principal.WindowsIdentity
win = System.Security.Principal.WindowsIdentity.GetCurrent()
LoggedInUserName = win.Name.Substring(win.Name.IndexOf("\") + 1)


'below code is for check the user name in LDAP

'object for Directory Search
Dim objDirSearcher As DirectorySearcher = Nothing

'object for result of Search
Dim objResult As SearchResultCollection = Nothing

'LDAP server name which is defined in app.config file
Dim ldapServer As String = ConfigurationManager.AppSettings(clsConstant.LDAPServer)

'LDAP Domain name which is defined in app.config file
Dim ldapDomain As String = ConfigurationManager.AppSettings(clsConstant.LDAPDomain)

Check_If_Member_Of_AD_Group = False

'search the directory
objDirSearcher = New DirectorySearcher("LDAP://" & ldapServer)

'Filter Directory by passing above loggedInUSerName
objDirSearcher.Filter = String.Format("(&(objectClass=user)(sAMAccountName={0}))", LoggedInUserName)

'get the result
objResult = objDirSearcher.FindAll()

If (objResult.Count > 0) Then
Return True
else
Return False
End If



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: