473,802 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User impersonation in C#


Hi,

I am using a win32 function, LogonUser, to impersonate a user across a WCF
service. I have noticed that everytime I call this function, a new logon
session is created for that user, meaning that multiple calls yields multiple
sessions. I cache a WindowsIdentity object that represents the user so that I
can call the Impersonate method whenever I need to without creating a new
session, but I really need to be able to log the user off when they close the
client application to avoid leaving open logon sessions. I've tried calling
ExitWindowsEx with a LogOff flag while impersonating as the relevant user,
but although the call seems to be successful (according to
Marshal.GetLast Win32Error()) it leaves the user logged in. However, closing
my WCF service DOES log the user off.

Can anyone tell me how I can log a specific user off? Also, is it the case
that a user is logged off if the process that created the logon is closed?

Cheers,

Chris
May 2 '07 #1
2 13431
Are you using a WindowsIdentity instance to Impersonate and Undo
Impersonation?
See
http://msdn2.microsoft.com/en-us/lib...afixedidentity

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Chris" <Ch***@discussi ons.microsoft.c omwrote in message
news:42******** *************** ***********@mic rosoft.com...
>
Hi,

I am using a win32 function, LogonUser, to impersonate a user across a WCF
service. I have noticed that everytime I call this function, a new logon
session is created for that user, meaning that multiple calls yields
multiple
sessions. I cache a WindowsIdentity object that represents the user so
that I
can call the Impersonate method whenever I need to without creating a new
session, but I really need to be able to log the user off when they close
the
client application to avoid leaving open logon sessions. I've tried
calling
ExitWindowsEx with a LogOff flag while impersonating as the relevant user,
but although the call seems to be successful (according to
Marshal.GetLast Win32Error()) it leaves the user logged in. However,
closing
my WCF service DOES log the user off.

Can anyone tell me how I can log a specific user off? Also, is it the case
that a user is logged off if the process that created the logon is closed?

Cheers,

Chris

May 3 '07 #2

Hi Kevin,

Thanks for your reply.

Yes, I'm doing:

using (identity.Imper sonate())
{...}

However, I have solved this problem literally 5 minutes ago - even when
closing the handle to the token I obtain from LogonUser, it seems that the
identity object maintains it's own handle. I tried to close this explicity,
but was only able to get the impersonated user logged off straight away by
actually triggering a garbage collection!

Cheers,

Chris
"Kevin Spencer" wrote:
Are you using a WindowsIdentity instance to Impersonate and Undo
Impersonation?
See
http://msdn2.microsoft.com/en-us/lib...afixedidentity

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Chris" <Ch***@discussi ons.microsoft.c omwrote in message
news:42******** *************** ***********@mic rosoft.com...

Hi,

I am using a win32 function, LogonUser, to impersonate a user across a WCF
service. I have noticed that everytime I call this function, a new logon
session is created for that user, meaning that multiple calls yields
multiple
sessions. I cache a WindowsIdentity object that represents the user so
that I
can call the Impersonate method whenever I need to without creating a new
session, but I really need to be able to log the user off when they close
the
client application to avoid leaving open logon sessions. I've tried
calling
ExitWindowsEx with a LogOff flag while impersonating as the relevant user,
but although the call seems to be successful (according to
Marshal.GetLast Win32Error()) it leaves the user logged in. However,
closing
my WCF service DOES log the user off.

Can anyone tell me how I can log a specific user off? Also, is it the case
that a user is logged off if the process that created the logon is closed?

Cheers,

Chris


May 3 '07 #3

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

Similar topics

2
1433
by: darkelf | last post by:
Hello, I have a site that uses windows authentication and I demand credentials for files inside a specific folder. (through web.config). My problem is that when I try to read something from my access database with a user that is not yet authorized I always get an error. (I am using a datareader with oledb that works perfectly for authorized users). I do not want to give access to unauthorized users for the specific folder for security...
1
1170
by: Itai Raz | last post by:
This used to work for us under Windows 2000 server, but doesn't seem to work with Windows 2003. We are accessing an SQL Server using integrated security from ASP.Net. For some reason it seems like the user that ASP.Net is using to connect to the DB is domain\machineName$. I don't understand why that is, since I have an account set up for the IIS app to run under, and i have impersonation set to true in the web.config file for the app....
2
7231
by: Sven Eric | last post by:
Hi All, I try to use user-impersonation in VB.NET with a user (let's call him Bill). I'm using the LogonUser function to create the token and so on. This works --> calling WindowsIdentity.GetCurrent().Name() afterwards gives me the username I'm try to impersonate. But this is about all I can do, I don't have access to the users files on the network, for instance I get an access denied error when I try to open a file on...
27
6716
by: vipleo | last post by:
I am having some issues, when I try to launch another process using Process.Start(ProcessStartInfo psi) on win xp sp2 box (Other versions of xp have no issue). Here is the detail. Main app checks for updates on startup and if updates are available, it launches separate exe to copy files. Before launching new process(exe), I am impersonating admin user as main app is being launched by non-admin user.
8
3521
by: Marco Mechelli | last post by:
Hello, i'm facing with the following problem while using the Job API during an impersonation. I have a main process that needs to do the following: 1. Creates a new Job Object that will be used to handle its children. 2. Create a new process (either by CreateProcessW() or by CreateProcessWithLogonW() for user impersonation).
4
7095
by: =?Utf-8?B?WmFyYm9yZw==?= | last post by:
I'm writing a little program that will run when a user logs in, checks their password expiration and also installs a piece of monitoring software if necessary. The program has to run on Vista so I got my first experience writing for UAC. I've worked though the signed manifest and all the other little quirky stuff I have to do to get my app to run as Administrator (System.DirectoryServices said it required admin rights) and everything is...
0
10535
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10303
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10282
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10061
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9111
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.