C# 使用指定的用户名和密码运行 mstsc.exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11296819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:17:50  来源:igfitidea点击:

Run mstsc.exe with specified username and password

c#rdp

提问by Krzysiek

I realize that in Windows 7, it is not possible to save different credentials for the same host, but I need some workaround.

我意识到在 Windows 7 中,不可能为同一主机保存不同的凭据,但我需要一些解决方法。

Can I provide the username and password manually in the code? Store them in a temp .rdp file?

我可以在代码中手动提供用户名和密码吗?将它们存储在临时 .rdp 文件中?

采纳答案by Krzysiek

Process rdcProcess = new Process();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe");
rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "username" +  " /pass:" + "password";
rdcProcess.Start();

rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.Arguments = "/v " + "192.168.0.217"; // ip or name of computer to connect
rdcProcess.Start();

The above code initiates a connection with .217 and I am not being prompted to provide a password. Thanks for help.

上面的代码启动了与 .217 的连接,并且没有提示我提供密码。感谢帮助。

回答by test

While trying to figure out how to allow users into our network, without giving them the keys to the castle, I enabled Remote Desktop Access for a few members of my team. Thinking more about this, I quickly remembered a project several years ago while working for the Department of Defense. That project required us to "lock down" access to only necessary personnel and limited access to the programs on the servers. After spending some time on Microsoft's KnowledgeBase, we realized that we could create desktop "shortcuts" for those employees that made the RDP connection, logged them in and limited their access to one specific application on that server.

在试图弄清楚如何允许用户进入我们的网络时,而不给他们城堡的钥匙,我为我的团队中的一些成员启用了远程桌面访问。想到这里,我很快想起了几年前在国防部工作时的一个项目。该项目要求我们“锁定”仅对必要人员的访问权限,并限制对服务器上程序的访问权限。在 Microsoft 的知识库上花了一些时间后,我们意识到我们可以为那些建立 RDP 连接的员工创建桌面“快捷方式”,让他们登录并限制他们访问该服务器上的一个特定应用程序。

回答by Sam Stephenson

If you want to use powershell you could add the credentials using

如果您想使用 powershell,您可以使用添加凭据

cmdkey /generic:DOMAIN/"computername or IP" /user:"username" /pass:"password"

Then call RDP connection using

然后使用调用 RDP 连接

Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/v:computer name/IP" -Wait

If you want to delete the credentials run

如果要删除凭据运行

cmdkey /delete:DOMAIN/"Computer name or IP"

Remember to remove ""

记得去掉“”

回答by Spencer

This is an updated version from Krzysiek's post.

这是 Krzysiek 帖子的更新版本。

var rdcProcess = new Process
    {
        StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = String.Format(@"/generic:TERMSRV/{0} /user:{1} /pass:{2}", 
                            fp.ipAddress,
                            (String.IsNullOrEmpty(fp.accountDomain)) ? fp.accountUserName : fp.accountDomain + "\" + fp.accountUserName,
                            fp.accountPassword),
                            WindowStyle = ProcessWindowStyle.Hidden                                
            }
    };
rdcProcess.Start();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
rdcProcess.StartInfo.Arguments = String.Format("/f /v {0}", fp.ipAddress); // ip or name of computer to connect
rdcProcess.Start();

回答by egl35720

@echo off

cmdkey /generic:TERMSRV/"*IP or Server Name*" /user:%username%

start mstsc /v:*IP or Server Name*

cmdkey /delete:TERMSRV/"*IP or Server Name*"

quit

回答by Sidney

The accepted answer solves the problem, but has the side effect of leaving the credentials in the users credential store. I wound up creating an IDisposable so I can use the credentials in a using statement.

接受的答案解决了问题,但会产生将凭据留在用户凭据存储中的副作用。我最终创建了一个 IDisposable 以便我可以在 using 语句中使用凭据。

using (new RDPCredentials(Host, UserPrincipalName, Password))
{
    /*Do the RDP work here*/
}


internal class RDPCredentials : IDisposable
{
    private string Host { get; }

    public RDPCredentials(string Host, string UserName, string Password)
    {
        var cmdkey = new Process
        {
            StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = $@"/list",
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };
        cmdkey.Start();
        cmdkey.WaitForExit();
        if (!cmdkey.StandardOutput.ReadToEnd().Contains($@"TERMSRV/{Host}"))
        {
            this.Host = Host;
            cmdkey = new Process
            {
                StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = $@"/generic:TERMSRV/{Host} /user:{UserName} /pass:{Password}",
                WindowStyle = ProcessWindowStyle.Hidden
            }
            };
            cmdkey.Start();
        }
    }

    public void Dispose()
    {
        if (Host != null)
        {
            var cmdkey = new Process
            {
                StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = $@"/delete:TERMSRV/{Host}",
                WindowStyle = ProcessWindowStyle.Hidden
            }
            };
            cmdkey.Start();
        }
    }
}

回答by Proxytype

most of the answers are incorrect, it still request password and this because execute different processes on the same process instance.

大多数答案都不正确,它仍然需要密码,这是因为在同一个流程实例上执行不同的流程。

using command line works perfectly:

使用命令行完美地工作:

        string command = "/c cmdkey.exe /generic:" + ip 
        + " /user:" + user + " /pass:" + password + " & mstsc.exe /v " + ip;

        ProcessStartInfo info = new ProcessStartInfo("cmd.exe", command);
        info.WindowStyle = ProcessWindowStyle.Hidden;
        info.CreateNoWindow = true;

        Process proc = new Process();
        proc.StartInfo = info;
        proc.Start();