User impersonation in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.GetLastWin32Error()) 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
 
Hi Kevin,

Thanks for your reply.

Yes, I'm doing:

using (identity.Impersonate())
{...}

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
 
Back
Top