我正在使用“Microsoft 终端服务控件类型库”来建立与远程桌面服务器的连接。我正在寻找一种方法来阻止或抑制在连接到使用网络级身份验证 (NLA) 的远程桌面服务器时未能提供正确的用户名/密码组合时显示的“Windows 安全”提示。窗口看起来像这样:
我已经阅读并尝试了目前可以在网上找到的所有设置组合,但没有一个成功。以下是我在 stackoverlow 上发现的几个问题,它们讨论了这个确切的问题,据说可以解决,但答案对我不起作用:
AxMsRdpClient6NotSafeForScripting AllowPromptingForCredentials
这听起来可能很荒谬,但我的最终目标只是尝试连接到 rdp 服务器并故意输入无效的用户名/密码,然后在失败时断开连接。我不关心实际连接或显示任何东西。如果这很重要,我这样做是为了在远程服务器上的事件日志中触发失败的登录尝试,稍后另一个应用程序将使用该登录尝试。
下面的代码已经在事件日志中触发了一次失败的登录尝试,但我只是找不到阻止这个失败的登录框在客户端计算机上弹出的方法,我宁愿不求助于尝试关闭窗口之后的黑客攻击是打开。当远程桌面服务器配置为允许来自运行任何版本的远程桌面的计算机的连接(不太安全的选项)时,我没有同样的问题,因为弹出提示显然是 NLA 提供的额外安全性的一部分。
我已经为此控件尝试了许多不同的设置组合,以至于我的头都在旋转。这是一个示例,它以上述其他 stackoverflow 问题之一为模型:
Public Class Form1
Dim WithEvents oRemote As AxMSTSCLib.AxMsRdpClient6NotSafeForScripting
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
oRemote = New AxMSTSCLib.AxMsRdpClient6NotSafeForScripting
CType(oRemote, System.ComponentModel.ISupportInitialize).BeginInit()
oRemote.Dock = System.Windows.Forms.DockStyle.Fill
oRemote.Enabled = True
oRemote.Name = "OfficeWin7"
Me.Controls.Add(oRemote)
CType(oRemote, System.ComponentModel.ISupportInitialize).EndInit()
oRemote.CreateControl()
oRemote.Size = New System.Drawing.Size(800, 600)
oRemote.Server = "IPADDRESS"
oRemote.UserName = "TestAccount"
oRemote.AdvancedSettings7.ClearTextPassword = "WrongPassword"
Dim ocx As MSTSCLib.IMsRdpClientNonScriptable4 = oRemote.GetOcx()
ocx.EnableCredSspSupport = True
ocx.AllowCredentialSaving = False
ocx.PromptForCredentials = False
ocx.PromptForCredsOnClient = False
oRemote.Connect()
End Sub
Private Sub oRemote_OnAuthenticationWarningDismissed(sender As Object, e As EventArgs) Handles oRemote.OnAuthenticationWarningDismissed
MessageBox.Show("The credentials popup is now closing")
End Sub
Private Sub oRemote_OnAuthenticationWarningDisplayed(sender As Object, e As EventArgs) Handles oRemote.OnAuthenticationWarningDisplayed
MessageBox.Show("The credentials popup is about to be shown")
End Sub
End Class
据说它是应该阻止此弹出窗口的行,但如果将该值设置为或ocx.PromptForCredentials = False似乎没有什么不同。我几乎会通过可能实际工作的属性名称来假设,但同样,我设置该值并没有什么不同。我总是得到相同的弹出窗口。TrueFalseocx.PromptForCredsOnClient
在这一点上,我不知道自己做错了什么,但我的直觉告诉我,要使其正常工作,我需要将基础AxMsRdpClient6NotSafeForScripting对象实例化为其他类似的东西AxMsRdpClient9NotSafeForScripting,甚至AxMsTscAxNotSafeForScripting是当我将控件拖放到表单上时控件使用的默认类型. 但是,我已经尝试了一堆这些设置组合,并希望有人能对这种情况有所了解。
我还应该提到,我对使用 .Net 连接到远程桌面服务器的替代方法持开放态度,这些方法不涉及使用(Microsoft Terminal Services Control Type Library如果有的话)。如果它们确实存在,我没有太多运气找到它们,但如果我在搜索中遗漏了任何内容,请告诉我。
编辑:为确保您自己的服务器设置与我的相同或相似,只需满足两个要求:
- 远程桌面必须在 Vista 或更高版本的 Windows 上运行
- 远程桌面必须设置为使用 NLA。在 Win7 中,确切的选项文本是:“仅允许来自运行具有网络级身份验证的远程桌面的计算机的连接(更安全)”
那时我不在乎您在代码中更改了哪些选项,只要服务器在您尝试连接时记录失败的登录并且凭据框(或任何其他弹出窗口)永远不会出现在客户端。
似乎添加此代码所需的正确引用的最简单方法是将 COM 选项卡中的“Microsoft 终端服务控件”添加到您的工具箱,然后将“Microsoft RDP 客户端控件”拖放到表单上。更多信息在这里:http ://s.codeproject.com/Articles/43705/Remote-Desktop-using-C-NET
以下是 C# 中的相同代码,以防使这个问题更受欢迎:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private AxMSTSCLib.AxMsRdpClient6NotSafeForScripting withEventsField_oRemote;
public AxMSTSCLib.AxMsRdpClient6NotSafeForScripting oRemote {
get { return withEventsField_oRemote; }
set {
if (withEventsField_oRemote != null) {
withEventsField_oRemote.OnAuthenticationWarningDismissed -= oRemote_OnAuthenticationWarningDismissed;
withEventsField_oRemote.OnAuthenticationWarningDisplayed -= oRemote_OnAuthenticationWarningDisplayed;
}
withEventsField_oRemote = value;
if (withEventsField_oRemote != null) {
withEventsField_oRemote.OnAuthenticationWarningDismissed += oRemote_OnAuthenticationWarningDismissed;
withEventsField_oRemote.OnAuthenticationWarningDisplayed += oRemote_OnAuthenticationWarningDisplayed;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
oRemote = new AxMSTSCLib.AxMsRdpClient6NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)oRemote).BeginInit();
oRemote.Dock = System.Windows.Forms.DockStyle.Fill;
oRemote.Enabled = true;
oRemote.Name = "OfficeWin7";
this.Controls.Add(oRemote);
((System.ComponentModel.ISupportInitialize)oRemote).EndInit();
oRemote.CreateControl();
oRemote.Size = new System.Drawing.Size(800, 600);
oRemote.Server = "IPADDRESS";
oRemote.UserName = "TestAccount";
oRemote.AdvancedSettings7.ClearTextPassword = "WrongPassword";
MSTSCLib.IMsRdpClientNonScriptable4 ocx = (MSTSCLib.IMsRdpClientNonScriptable4)oRemote.GetOcx();
ocx.EnableCredSspSupport = true;
ocx.AllowCredentialSaving = false;
ocx.PromptForCredentials = false;
ocx.PromptForCredsOnClient = false;
oRemote.Connect();
}
private void oRemote_OnAuthenticationWarningDismissed(object sender, EventArgs e)
{
MessageBox.Show("The credentials popup is now closing");
}
private void oRemote_OnAuthenticationWarningDisplayed(object sender, EventArgs e)
{
MessageBox.Show("The credentials popup is about to be shown");
}
public Form1()
{
Load += Form1_Load;
}
}
}
