Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Impersonate problem (http://www.velocityreviews.com/forums/t112753-impersonate-problem.html)

=?Utf-8?B?QmFsYQ==?= 11-02-2005 11:57 PM

Impersonate problem
 
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

=?Utf-8?B?QmFsYQ==?= 11-03-2005 12:20 AM

RE: Impersonate problem
 
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



All times are GMT. The time now is 02:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.