远程桌面连接-C#事件



遇到一个小问题。

我们有一个来自Netgear的面向互联网的VPN,允许教职员工和教师在家中使用RDC访问学校网络。

他们使用网络浏览器登录VPN,点击我们的一个远程服务器,他们就被RDC’ed了。

然而,人们有一个巨大的问题,注销。这似乎是他们的想法。所有用户所做的就是点击RDC客户端上的关闭按钮,而不是将他们注销

我们正在构建一个程序来解决这个问题,想法是"挂钩"到远程桌面API,然后检查会话是否断开,如果断开,我们注销用户。

该程序将作为服务或物理最小化EXE在后台运行。

我们正在C#中构建它。那么,有人知道可以使用.NET4调用的RDC事件吗?它将允许我们知道用户何时关闭会话。

如果你需要更多的信息,请告诉我。

干杯

明白了。

呼叫

SystemEvents.SessionSwitch += new SessionSwitchEventHandle SystemEvents_SessionSwitch);

然后

static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.RemoteDisconnect || e.Reason == SessionSwitchReason.ConsoleDisconnect)
{
// Log off the user...
}
else
{
// Physical Logon
}
}

这比上面的其他答案效果更好。

public Form1()
{
InitializeComponent();
if (!WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
}
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);
[DllImport("WtsApi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)]int dwFlags);
[DllImport("WtsApi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
// constants that can be passed for the dwFlags parameter
const int NOTIFY_FOR_THIS_SESSION = 0;
const int NOTIFY_FOR_ALL_SESSIONS = 1;
// message id to look for when processing the message (see sample code)
const int WM_WTSSESSION_CHANGE = 0x2b1;
// WParam values that can be received: 
const int WTS_CONSOLE_CONNECT = 0x1; // A session was connected to the console terminal.
const int WTS_CONSOLE_DISCONNECT = 0x2; // A session was disconnected from the console terminal.
const int WTS_REMOTE_CONNECT = 0x3; // A session was connected to the remote terminal.
const int WTS_REMOTE_DISCONNECT = 0x4; // A session was disconnected from the remote terminal.
const int WTS_SESSION_LOGON = 0x5; // A user has logged on to the session.
const int WTS_SESSION_LOGOFF = 0x6; // A user has logged off the session.
const int WTS_SESSION_LOCK = 0x7; // A session has been locked.
const int WTS_SESSION_UNLOCK = 0x8; // A session has been unlocked.
const int WTS_SESSION_REMOTE_CONTROL = 0x9; // A session has changed its remote controlled status.
protected override void OnHandleDestroyed(EventArgs e)
{
// unregister the handle before it gets destroyed
WTSUnRegisterSessionNotification(this.Handle);
base.OnHandleDestroyed(e);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_WTSSESSION_CHANGE)
{
int value = m.WParam.ToInt32();
if (value == WTS_REMOTE_DISCONNECT)
{
ExitWindowsEx(4, 0); // Logout the user on disconnect
}
else if (value == WTS_REMOTE_CONNECT)
{
MessageBox.Show("Welcome to the VPN. There is no need to Logout anymore, as when you close this session it will automatically log you out");
}
}
base.WndProc(ref m);
}

相关内容

  • 没有找到相关文章

最新更新