如何以编程方式从 Visual Basic 登录到终端服务器会话

概要

Terminal Services ActiveX 客户机控件未公开ImsTscNonScriptable 接口。但是,可以使用该接口通过编程方式为终端服务会话配置自动登录,这使得程序员能够让用户直接登录到终端服务会话,而不会出现“Windows 登录”提示窗口。“详细信息”部分的示例演示了如何实现此功能。

更多信息

若要运行该程序,请按照以下方式配置终端服务器:
1. 以管理员身份从本地登录到终端服务器。
2. 开始菜单上,单击程序,单击管理工具,然后单击终端服务配置
3. 单击连接
4. 在右窗格中,右键单击RDP-Tcp,然后选择属性
5. 单击登录设置选项卡。
6. 取消选择总是提示密码,然后单击确定
备注:出于安全考虑,Microsoft 建议您在未考虑周密或不清楚了解 Microsoft Windows 安全机制的情况下,不要轻易实施本方案。

代码示例

1. 启动一个新的标准 EXE 工程。默认情况下将创建 Form1。
2. 工程菜单上,单击以选择组件,选择Microsoft Terminal Services Control(redist),然后单击确定。如果该控件不可用,请参见本文中的“参考”部分,了解有关如何下载和安装该控件的信息。
3. 在 Form1 上添加一个 Terminal Services 控件。确保它足够大,能够显示会话窗口。
4. 在 Form1 上添加三个Label 控件,三个TextBox 控件,和一个CommandButton 控件。确保 Lable1 和 Text1 在同一行上,Label2、Text2、Label3 和 Text3 在同一行上。
5. 将以下代码粘贴到 Form1 的通用声明中:

' This code only works when you set the configuration on the Server-side.
' Log on to the Terminal Server as an administrator
' Start\Programs\Administrative Tools\Terminal Services Configuration
' Click on Connections
' On the Right Pane, right-click on RDP-Tcp and choose Properties
' Click on the "Logon Settings" Tab
' Uncheck "Always prompt for password" and click OK
Option Explicit
Private Obj As IMsTscNonScriptable
Private Sub Form_Load()
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  Label1.Caption = "Server"
  Label2.Caption = "UserName"
  Label3.Caption = "Password"
  Command1.Caption = "Connect"
  Text3.PasswordChar = "*"
End Sub
Private Sub Command1_Click()
  Set Obj = MsTscAx1.Object
  MsTscAx1.Server = Text1.Text
  MsTscAx1.UserName = Text2.Text
  Obj.ClearTextPassword = Text3.Text
  MsTscAx1.Connect
End Sub
6. 保存该项目,按下 F5 键运行该程序。请注意,在您输入了用户名、密码和服务器名后,不会出现登录屏幕,提示您登录服务器。Microsoft 建议您扩展终端服务 .ocx 文件,这样您就能够操作关闭对话框了。
原文地址:https://www.cnblogs.com/hhq80/p/620015.html