Impersonation Problem

Discussion in 'ASP .Net' started by velvet.graham@gmail.com, Mar 29, 2006.

  1. Guest

    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.
    , Mar 29, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelly D. Jones

    Problem with impersonation and delegation

    Kelly D. Jones, Sep 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,821
  2. Thomas

    problem using impersonation

    Thomas, Oct 24, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    449
    Thomas
    Oct 27, 2003
  3. jm
    Replies:
    1
    Views:
    1,926
    bruce barker
    Dec 20, 2003
  4. Replies:
    0
    Views:
    408
  5. hellrazor

    problem with aspnet "impersonation"

    hellrazor, Nov 19, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,810
    Willy Denoyette [MVP]
    Nov 19, 2004
Loading...

Share This Page