Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Impersonate problem

Reply
Thread Tools

Impersonate problem

 
 
=?Utf-8?B?QmFsYQ==?=
Guest
Posts: n/a
 
      11-02-2005
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.WindowsId entity.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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QmFsYQ==?=
Guest
Posts: n/a
 
      11-03-2005
exact error message is :
Token cannot be zero


"Bala" wrote:

> 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.WindowsId entity.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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
please help + impersonate problem =?Utf-8?B?QmFsYQ==?= ASP .Net 1 11-04-2005 12:30 AM
Problem with impersonate Yoshitha ASP .Net 0 07-01-2005 07:12 AM
ASP.NET Webservice Impersonate problem Aras Kucinskas ASP .Net 0 08-31-2004 02:07 PM
DirectoryEntry Impersonate or WindowsIdentity Impersonate? Bill Belliveau ASP .Net Security 3 01-31-2004 04:19 AM
network (drive) access/impersonate identity problem Peter O'Reilly ASP .Net 2 11-03-2003 09:14 PM



Advertisments