Hi,
I've problem with LDAP authentication of my asp.net application since it was a intranet application my job is to get authenticated using active directory (LDAP). For this i've using the below class.
Public Class LDAP
Public Const SECURITY_IMPERSONATION_LEVEL_SecurityAnonymous As Integer = 0
Public Const SECURITY_IMPERSONATION_LEVEL_SecurityIdentification As Integer = 1
Public Const SECURITY_IMPERSONATION_LEVEL_SecurityImpersonation As Integer = 2
Public Const SECURITY_IMPERSONATION_LEVEL_SecurityDelegation As Integer = 3
Public Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Public Const LOGON32_PROVIDER_WINNT35 As Integer = 1
Public Const LOGON32_PROVIDER_WINNT40 As Integer = 2
Public Const LOGON32_PROVIDER_WINNT50 As Integer = 3
Public Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Public Const LOGON32_LOGON_NETWORK As Integer = 3
Public Const LOGON32_LOGON_BATCH As Integer = 4
Public Const LOGON32_LOGON_SERVICE As Integer = 5
Public Const LOGON32_LOGON_UNLOCK As Integer = 7
Public Const LOGON32_LOGON_NETWORK_CLEARTEXT As Integer = 8
Public Const LOGON32_LOGON_NEW_CREDENTIALS As Integer = 9
Public Const ERROR_LOGON_FAILURE As Integer = 1326
<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function RevertToSelf() As Boolean
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean
End Function
<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function DuplicateToken(ByVal hToken As IntPtr, ByVal impersonationLevel As Integer, ByRef hNewToken As IntPtr) As Integer
End Function
Public Shared Function Login(ByVal Username As String, ByVal Password As String) As Boolean
Dim secPerm As New SecurityPermission(SecurityPermissionFlag.UnmanagedCode)
secPerm.Assert()
Dim user As WindowsIdentity = Nothing
Dim refToken As IntPtr = IntPtr.Zero
Dim loggedIn As Boolean
loggedIn = LDAP.LogonUser(Username, "LDAP://LDAP connection string";, Password, LDAP.LOGON32_LOGON_NETWORK_CLEARTEXT, LDAP.LOGON32_PROVIDER_DEFAULT, refToken)
Return loggedIn
End Function
End Class
It works well if i run this locally, but it doesn't connect while hosting in IIS7.5. I've searched my best in google but the alternatives they've given was not works for me. Kindly take a look at this and revert your suggestions.
My Ref:
http://msdn.microsoft.com/en-us/library/gg703322(v=vs.98).aspx[
^]
http://stackoverflow.com/questions/4505231/impersonate-user-in-codebehind[
^]
Thanks & Regards,
BlueSathish