Impersonate problem


G

Guest

Hi

I am using below code for impersonating. Previosualy it was working fine.

Now its giving this error message. any idea how to resolve this.

LogonUser failed with error code : 87

thanks
bala

code:

Dim impContext As WindowsImpersonationContext = Nothing
Try
'
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name())
impContext = NetworkSecurity.ImpersonateUser("WEB",
Session("WinLogin"), Session("WinPassword"), LogonType.LOGON32_LOGON_NETWORK,
LogonProvider.LOGON32_PROVIDER_DEFAULT)

Catch ex As ApplicationException
' impContext.Undo()
Response.Write(ex.Message)
End Try





Public Shared Function ImpersonateUser(ByVal strDomain As String, ByVal
strLogin As String, ByVal strPwd As String, ByVal logonType As LogonType,
ByVal logonProvider As LogonProvider) As WindowsImpersonationContext
Dim tokenHandle As IntPtr = New IntPtr(0)
Dim dupeTokenHandle As IntPtr = New IntPtr(0)
Try
Const SecurityImpersonation As Integer = 2
tokenHandle = IntPtr.Zero
dupeTokenHandle = IntPtr.Zero
Dim returnValue As Boolean = SecuUtil32.LogonUser(strLogin,
strDomain, strPwd, CType(logonType, Integer), CType(logonProvider, Integer),
tokenHandle)
If False = returnValue Then
Dim ret As Integer = Marshal.GetLastWin32Error
Dim strErr As String = String.Format("LogonUser failed
with error code : {0}", ret)
Throw New ApplicationException(strErr, Nothing)
End If
Dim retVal As Boolean =
SecuUtil32.DuplicateToken(tokenHandle, SecurityImpersonation, dupeTokenHandle)
If False = retVal Then
SecuUtil32.CloseHandle(tokenHandle)
Throw New ApplicationException("Failed to duplicate
token", Nothing)
End If
Dim newId As WindowsIdentity = New
WindowsIdentity(dupeTokenHandle)
Dim impersonatedUser As WindowsImpersonationContext =
newId.Impersonate
Return impersonatedUser
Catch ex As Exception
Throw New ApplicationException(ex.Message, ex)
End Try
Return Nothing
End Function
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top