使用在锁定的工作站上打开的应用程序登录到Windows 7

时间:2011-03-13 16:01:24

标签: c# authentication windows-7

我在c#中写了一个小应用程序。我希望这个应用程序锁定我自己登录时解锁工作站。

锁定工作站非常简单,工作正常。

当我想要解锁工作站时,问题就开始了。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Security.Principal;
using System.Security.Permissions;
using System.Drawing;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    [DllImport("User32.dll")]
    public static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("User32.dll")]
    public static extern void ReleaseDC(IntPtr dc);

    IntPtr desktopDC;
    Graphics g;

    public static class Logon
    {
        [DllImport("User32.Dll", EntryPoint = "LockWorkStation"), Description("Locks the workstation's display. Locking a workstation protects it from unauthorized use.")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool LockWorkStation();

        /// <exception cref="Win32Exception">if the lock fails more information can be found in this Exception class</exception>
        public static void LockWorkstation()
        {
            if (!LockWorkStation())
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        [DllImport("advapi32.dll", EntryPoint = "LogonUser")]
        public static extern bool LogonUser(

        string lpszUsername,

        string lpszDomain,

        string lpszPassword,

        int dwLogonType,

        int dwLogonProvider,

        ref IntPtr phToken);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Logon.LockWorkstation();
        timer1.Start();   
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        Login();
    }

    private void Login()
    {
        string sUsername = "adam";
        string sDomain = System.Environment.MachineName;
        string sPassword = "sernik";

        const int LOGON32_PROVIDER_DEFAULT = 0;
        // create token

        const int LOGON32_LOGON_INTERACTIVE = 2;
        //const int SecurityImpersonation = 2;


        IntPtr pExistingTokenHandle = new IntPtr(0);
        IntPtr pDuplicateTokenHandle = new IntPtr(0);
        pExistingTokenHandle = IntPtr.Zero;
        pDuplicateTokenHandle = IntPtr.Zero;


        bool a = Logon.LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);

        g.DrawString(a.ToString(), new Font(FontFamily.GenericSansSerif, 80), Brushes.Red, 100, 100);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Login();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        desktopDC = GetDC(IntPtr.Zero);

        g = Graphics.FromHdc(desktopDC);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        g.Dispose();
        ReleaseDC(desktopDC);
    }
}
}

我正在尝试使用LogonUser方法,但它只会提供我的真假结果,并且不会实际解锁屏幕。

如何在Windows 7下执行此操作?

应用程序的puprose是检测插入PC的电子钥匙的存在,然后锁定或解锁工作站。

1 个答案:

答案 0 :(得分:4)

  

应用程序的puprose是检测插入PC的电子钥匙的存在,然后锁定或解锁工作站。

在Windows Vista之前,您可以使用GINA执行此类操作,因为您提到Windows 7,您必须使用new infrastructure。无论哪种方式,只需在相关机器上运行程序就无法实现这种行为,您需要与Windows的身份验证系统联系起来。

相关问题