以编程方式解锁 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6975206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:43:40  来源:igfitidea点击:

Unlock Windows programmatically

c#windowslogin

提问by fdezjose

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

在我当前的 C# 代码中,我能够以编程方式锁定 Windows 用户会话(与 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.

由于应用程序仍在运行,有没有办法从该 C# 程序解锁会话。用户凭据是已知的。该应用程序在 Windows 7 上运行。

回答by Ivan Danilov

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

这里有一些技巧可以做到这一点:http: //www.codeproject.com/Articles/16197/Remotely-Unlock-a-Windows-Workstation虽然我自己没有测试过。

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.

不适用于 .NET 部分,但您也可以制作自己的自定义登录 UI 并在其中注入一些机制。虽然它很容易成为安全问题。

回答by toster-cx

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

您需要一个自定义的 Windows 凭据提供程序来为您登录。此外,您需要将用户的凭据保存在某处才能登录。 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.

有很多项目可以让您在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_STATUSmessage to see how to trigger the login
  • build some way to communicate with your app to trigger the unlock (local tcp server for example)
  • 设置用户名/密码 CSampleCredential::Initialize
  • 将自动登录设置为 true CSampleCredential::SetSelected
  • 在硬件供应商示例中搜索WM_TOGGLE_CONNECTED_STATUS消息以查看如何触发登录
  • 建立某种方式与您的应用程序通信以触发解锁(例如本地 tcp 服务器)

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

这是一个痛苦的屁股,但它的工作原理。

回答by user3059402

    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.

注意:这仅在您以管理员身份运行 Visual Studio 时有效。

回答by Joel Coehoorn

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.

当然不能解锁。解锁会话需要用户亲自到场以输入其帐户凭据。允许软件执行此操作,即使使用已保存的凭据,对于使用工作站锁定的许多其他情况也将是一个安全问题。

回答by Ana Betts

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

不,按照设计,没有办法做到这一点。您的情况是什么,为什么需要锁定/解锁工作站?