Unlock Windows programmatically

前端 未结 5 1813
栀梦
栀梦 2020-11-28 12:39

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 unlo

相关标签:
5条回答
  • 2020-11-28 13:07

    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.

    0 讨论(0)
  • 2020-11-28 13:07

    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.

    0 讨论(0)
  • 2020-11-28 13:15

    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.

    0 讨论(0)
  • 2020-11-28 13:18
        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.

    0 讨论(0)
  • 2020-11-28 13:19

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

    0 讨论(0)
提交回复
热议问题