473,394 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Impersonate problem

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.WindowsId entity.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
Nov 19 '05 #1
1 3062
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.WindowsId entity.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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Yoshitha | last post by:
Hi I've to access registry and also get cpu id from the web application. First i wrote code to get cpuid from web application then i got cpu id. Next i wrote code to set value into a registry and...
1
by: moemeelaung | last post by:
Hi ASP/Windows experts out there I really need help with this Impersonate function. I have machine A and B. A hosts my ASP page which is to create a user account on the machine B. The...
12
by: Nils M. Lunde | last post by:
Hi! I've made a Web Service using C# that is using impersonation. The WS is working fine on WinXP and Win2003Server, but I'm having problem getting it to work on Win2000. The problem is that...
8
by: BLiTZWiNG | last post by:
After playing with the code shown and utilising Willy Denyottes' help, I have come to the conclusion that there is some form of difference between the managed WindowsIdentity.Impersonate() over the...
6
by: Erez Shor | last post by:
Hi, I need to build and asp page which access a remote windows server's registry and create a registry key. In order for the ASP page to be able to access the registry on the remote server I need...
2
by: franzhe | last post by:
Hi all, in a simple ASP.Net application with resources in satellite assemblies I have the following problem: If I set <identity impersonate="true"/>, accessing a culture specific resource...
2
by: KaNos | last post by:
Hello world, I've made a webservice (c# v2) to install in a server IIS 6 on a Windows 2000 last SP. We can use the webservice in local, throw the pages wich present the methods, with a windows...
2
by: rockdale | last post by:
Hi, all: My asp.net application calles MS speech 5.1 and generate a wav file on server's path. Everything runs perfectly on my development machine. But when I move the appl to production server,...
4
by: =?Utf-8?B?QXZhRGV2?= | last post by:
ASP.Net 2. We are migrating to Windows 2008 64 bit Server with IIS 7 from Windows 2003 32 Bit with IIS 6. A few library classes we wrote uses impersonation in code like explained in this...
5
by: WT | last post by:
Hello, IIS6 on W2K3, .net 3.5, Sql 2005. All sp applied. My site is using windows authentication only and the web application connects to sql server residing on another server in the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.