Impersonate problem

Discussion in 'ASP .Net' started by =?Utf-8?B?QmFsYQ==?=, Nov 2, 2005.

  1. 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.WindowsIdentity.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==?=, Nov 2, 2005
    #1
    1. Advertising

  2. 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.WindowsIdentity.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==?=, Nov 3, 2005
    #2
    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. Peter O'Reilly
    Replies:
    2
    Views:
    10,860
    Peter O'Reilly
    Nov 3, 2003
  2. Aras Kucinskas

    ASP.NET Webservice Impersonate problem

    Aras Kucinskas, Aug 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,012
    Aras Kucinskas
    Aug 31, 2004
  3. Yoshitha

    Problem with impersonate

    Yoshitha, Jul 1, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    386
    Yoshitha
    Jul 1, 2005
  4. =?Utf-8?B?QmFsYQ==?=

    please help + impersonate problem

    =?Utf-8?B?QmFsYQ==?=, Nov 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    Joshua Mitts
    Nov 4, 2005
  5. Bill Belliveau

    DirectoryEntry Impersonate or WindowsIdentity Impersonate?

    Bill Belliveau, Jan 28, 2004, in forum: ASP .Net Security
    Replies:
    3
    Views:
    325
    Joe Kaplan \(MVP - ADSI\)
    Jan 31, 2004
Loading...

Share This Page