24

In my current C# code I'm able to lock a Windows user session programmatically (same as Windows + L).

Since the app would still be running, is there any way to unlock the session from that C# program. User credentials are known. The app is running on Windows 7.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
fdezjose
  • 607
  • 2
  • 9
  • 18

5 Answers5

7

You'll need a custom windows credential provider to log in for you. Also, you'll need to save the user's credentials somewhere to log in. There are some samples in Windows SDK 7 https://www.microsoft.com/en-us/download/details.aspx?id=8279

There's a bunch of projects to get you started under Samples\security\credentialproviders.

To unlock the screen:

  • set the username / password in CSampleCredential::Initialize
  • set autologin to true in CSampleCredential::SetSelected
  • search the hardware provider sample for WM_TOGGLE_CONNECTED_STATUS message to see how to trigger the login
  • build some way to communicate with your app to trigger the unlock (local tcp server for example)

It's a pain in the ass, but it works.

toster-cx
  • 2,287
  • 1
  • 26
  • 32
  • I'd like to get this solution working on Windows 10 but my Windows skills are rusty. Please contact me through the site on my profile page if you'd be interested in doing some consulting work here. – davidgyoung Jun 18 '18 at 14:37
5

Here is some hackery to do that: http://www.codeproject.com/Articles/16197/Remotely-Unlock-a-Windows-Workstation Didn't test it myself though.

Not for .NET part, but you could also make your own custom Logon UI and inject some mechanism there. It can easily become security problem though.

Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
-2
    var path = new ManagementPath();
    path.NamespacePath = "\\ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption"; path.ClassName = "Win32_EncryptableVolume";

    var scope = new ManagementScope(path, new ConnectionOptions() { Impersonation = ImpersonationLevel.Impersonate });

    var management = new ManagementClass(scope, path, new ObjectGetOptions());

    foreach (ManagementObject vol in management.GetInstances())
    {

        Console.WriteLine("----" + vol["DriveLetter"]);
        switch ((uint)vol["ProtectionStatus"])
        {
            case 0:
                Console.WriteLine("not protected by bitlocker");
                break;
            case 1:
                Console.WriteLine("unlocked");
                break;
            case 2:
                Console.WriteLine("locked");
                break;
        }

        if ((uint)vol["ProtectionStatus"] == 2)
        {
            Console.WriteLine("unlock this driver ...");

            vol.InvokeMethod("UnlockWithPassphrase", new object[] { "here your pwd" });

            Console.WriteLine("unlock done.");
        }
    }

Note: this only works if you run Visual Studio as an administrator.

David Moles
  • 48,006
  • 27
  • 136
  • 235
-13

No, there is no way to do this, by design. What's your scenario and why do you need to lock/unlock the workstation?

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 3
    It can be done on Vista and windows 7 defiantly. Look at logmein and face recognition logins; I don't know how to do it though – Will03uk Aug 07 '11 at 19:38
  • 1
    @Will03uk: Those are done by writing a custom GINA DLL (which controls the login authentication process). – Greg Hewgill Aug 07 '11 at 19:54
  • 1
    I've just looked it up and since Vista the GINA DLL has been replaced with Credential Providers which allows more flexibility and more then one provider a time – Will03uk Aug 07 '11 at 23:21
  • That's not true. You can use a custom Credential provider. – Bemipefe May 19 '16 at 15:45
-14

Of course you can't unlock it. Unlocking a session requires the user physically be there to enter their account credentials. Allowing software to do this, even with saved credentials, would be a security issue for many of the other situations where workstation locking is used.

dtb
  • 213,145
  • 36
  • 401
  • 431
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794