5

I'd like to know if is there any way in C# to programatically log-in a Windows User Account?

We are currently developing an monitoring application and one of the feature is to be able to log-off the current Windows User and switch to another Windows User Account.

I have some few methods for Logging off, such as through API or Command Line. But what about logging-in a user account?

4
  • Um. Where is your code running? If you're running as an application as the current user, your application will be terminated by the log-off occurring. Commented Mar 12, 2010 at 7:35
  • 2
    @Damien_The_Unbeliever - Do you believe in Windows services? Commented Mar 12, 2010 at 7:47
  • @Earwicker - of course, but I'd appreciate some clarity, given it was described as an application, not a service (although this may just be a terminology issue) Commented Mar 12, 2010 at 7:50
  • I'd like to make a custom LogonUI for my VMs. P/Invoke will be accepted. Commented Jan 30, 2021 at 21:34

2 Answers 2

3

You mean interactive?

  • Logoff, if anything fails, spawn a request to logoff.exe
  • Logon - nope. that is a GINA module that can go in front (look it up in google), but this is a very tricky beast to do. The security system in windows is pretty well isolated for obvious reasons (i.e. security).

I am not aware of anything you could do in C#.

http://technet.microsoft.com/en-us/library/cc780332%28WS.10%29.aspx

has some more details how user logon works.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 It should be possible to replace the GINA module with your own.
1

This might be what you're looking for...codesample

5 Comments

not sure I would consider setting up auto login relevant here.
+1 - although that method also requires a reboot, it does work - the effect would be the same.
Whow. "althoug it requires a reboot, the effect would be the same" must be a new level of how low computer service can go. A solution reuiqring a eboot to change the logged in user is NOT the same.
Not an option in production systems.
@TomTom To complainers. Yes, at the moment I do not like this method either. And oh man, I am also disappointed by not having found what I was looking for :P But then again, the above answer is not pointless at all. I was looking for a solution to a different though related problem. For switching active user the above solution may be rather helpful in some cases. In case there is no better way, the above is a way. Just because you do not need this answer or it does not fit your needs and because you are disappointed, does not mean the answer is bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.