Impersonation Problem

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top