Impersonation Problem



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.

.



Relevant Pages

  • Re: Importing text, etc. on remote .mdb file
    ... You can then use VBA string functions to get from there to the ... Is there an easy way to schedule the imports of the text files so I ... 'Modify strSQL and filenames as required ... Dim oJet 'DAO.DBEngine ...
    (microsoft.public.access.externaldata)
  • RE: Logon Error
    ... > Imports CrystalDecisions.CrystalReports ... > function is emailPDFwhich calls ChangeLogOnInfo EmailPDF returns a Report ... > Dim szErrorMailSecondary As String 'This is where all the mail messages ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Eval code and AppDomains
    ... Do you know of any other way to Eval a String to get a Boolean result other ... >> Dim objEval As New EvalProvider ... >> Imports Microsoft.VisualBasic ...
    (microsoft.public.dotnet.framework.aspnet)
  • Logon Error
    ... OK why does the following code work in a Windows form with a crystal report ... Imports CrystalDecisions.CrystalReports ... Dim szErrorMailSecondary As String 'This is where all the mail messages ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Impersonate user from ASP.NET - access to network file share
    ... I have tried putting the impersonation code into a Class ... > Imports System.String ... > Shared Function impersonateValidUser(ByVal userName As String, ... > End Sub ...
    (microsoft.public.dotnet.general)

Loading