V
velvet.graham
I'm having a difficult time with impersonation. I've created an
impersonation class. Here is the code below:
******Impersonation Class Code*********
Imports System
Imports System.Web.Security
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.DirectoryServices
Imports System.Security.Permissions
Public Class SecurityHelpers
Private Sub New()
MyBase.New()
End Sub
Declare Auto Function LogonUser Lib "advapi32.dll" (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
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle
As IntPtr) As Boolean
Public Shared Function CreateIdentity(ByVal userName As String,
ByVal domain As String, ByVal password As String) As WindowsIdentity
Dim tokenHandle As IntPtr = New IntPtr(0)
Const LOGON32_PROVIDER_KERBEROS As Integer = 3
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
tokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userName, domain,
password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_KERBEROS,
tokenHandle)
If (False = returnValue) Then
Dim ret As Integer = Marshal.GetLastWin32Error
Throw New System.UnauthorizedAccessException(("LogonUser
failed with error code: " + ret))
End If
Dim id As WindowsIdentity = New WindowsIdentity(tokenHandle)
CloseHandle(tokenHandle)
Return id
End Function
End Class
*********Here is how I'm calling it in my code:*********
Dim wic As WindowsImpersonationContext = Nothing
wic = SecurityHelpers.CreateIdentity(username, domain,
password).Impersonate
'Upload the file
wic.undo
The error only occurs when I'm on trying to access resources on another
server. I can upload a file to the other server, but I can't access any
of the file's properties. Here is the error below:
********Error***********
Impersonation failure.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Impersonation failure.
Portion of stack trace:
HttpException (0x80004005): Impersonation failure.]
System.Web.ImpersonationSuspendContext.GetCurrentToken() +215
System.Web.ImpersonationSuspendContext.Suspend() +53
System.Web.HttpContext.GetConfig(String name) +104
System.Web.Configuration.HttpConfigurationSystemBase.System.Configuration.IConfigurationSystem.GetConfig(String
configKey) +22
System.Configuration.ConfigurationSettings.GetConfig(String
sectionName) +69
System.Configuration.ConfigurationSettings.get_AppSettings() +26
I would appreciate any help you could give me. Thanks.
impersonation class. Here is the code below:
******Impersonation Class Code*********
Imports System
Imports System.Web.Security
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.DirectoryServices
Imports System.Security.Permissions
Public Class SecurityHelpers
Private Sub New()
MyBase.New()
End Sub
Declare Auto Function LogonUser Lib "advapi32.dll" (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
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle
As IntPtr) As Boolean
Public Shared Function CreateIdentity(ByVal userName As String,
ByVal domain As String, ByVal password As String) As WindowsIdentity
Dim tokenHandle As IntPtr = New IntPtr(0)
Const LOGON32_PROVIDER_KERBEROS As Integer = 3
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
tokenHandle = IntPtr.Zero
Dim returnValue As Boolean = LogonUser(userName, domain,
password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_KERBEROS,
tokenHandle)
If (False = returnValue) Then
Dim ret As Integer = Marshal.GetLastWin32Error
Throw New System.UnauthorizedAccessException(("LogonUser
failed with error code: " + ret))
End If
Dim id As WindowsIdentity = New WindowsIdentity(tokenHandle)
CloseHandle(tokenHandle)
Return id
End Function
End Class
*********Here is how I'm calling it in my code:*********
Dim wic As WindowsImpersonationContext = Nothing
wic = SecurityHelpers.CreateIdentity(username, domain,
password).Impersonate
'Upload the file
wic.undo
The error only occurs when I'm on trying to access resources on another
server. I can upload a file to the other server, but I can't access any
of the file's properties. Here is the error below:
********Error***********
Impersonation failure.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Impersonation failure.
Portion of stack trace:
HttpException (0x80004005): Impersonation failure.]
System.Web.ImpersonationSuspendContext.GetCurrentToken() +215
System.Web.ImpersonationSuspendContext.Suspend() +53
System.Web.HttpContext.GetConfig(String name) +104
System.Web.Configuration.HttpConfigurationSystemBase.System.Configuration.IConfigurationSystem.GetConfig(String
configKey) +22
System.Configuration.ConfigurationSettings.GetConfig(String
sectionName) +69
System.Configuration.ConfigurationSettings.get_AppSettings() +26
I would appreciate any help you could give me. Thanks.