簡體   English   中英

使用在鎖定工作站上打開的應用程序登錄到Windows 7

[英]Loging to Windows 7 using the application opened on a locked workstation

我在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的電子鑰匙的存在,然后鎖定或解鎖工作站。

應用程序的puprose是檢測插入PC的電子鑰匙的存在,然后鎖定或解鎖工作站。

在Windows Vista之前,你可以用GINA做這樣的事情,因為你提到Windows 7,你將不得不使用新的基礎設施 無論哪種方式,只需在相關機器上運行程序就無法實現這種行為,您需要與Windows的身份驗證系統聯系起來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM