如何以编程方式创建 Windows VPN 连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/318233/
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-09-09 05:46:55  来源:igfitidea点击:

How can I programmatically create a Windows VPN connection?

windowsvpn

提问by hurcane

I have a support environment where we connect to many of our customers' systems using VPN and Remote Desktop. Right now, support personnel have to manually set up the VPN connection on their workstation. Is there a way to programmatically create the VPN connection?

我有一个支持环境,我们使用 VPN 和远程桌面连接到许多客户的系统。现在,支持人员必须在他们的工作站上手动设置 VPN 连接。有没有办法以编程方式创建 VPN 连接?

采纳答案by hurcane

Check out the DotRas project on CodePlex, the RasPhoneBook component has full support over managing phone books used by Windows.

查看 CodePlex 上的 DotRas 项目,RasPhoneBook 组件完全支持管理 Windows 使用的电话簿。

http://www.codeplex.com/DotRas

http://www.codeplex.com/DotRas

It'll be a lot easier than working with the APIs directly, that much I can guarantee. :)

这比直接使用 API 容易得多,我可以保证这一点。:)

回答by ugurturhal

I know the question is very old but i had same issue and solve it with this C# code. Maybe it helps to somebody.

我知道这个问题很老,但我遇到了同样的问题并用这个 C# 代码解决了它。也许它对某人有帮助。

First, create vpn connection in your personal computer. It will be storage in %userprofile%\AppData\Roaming\Microsoft\Network\Connections\PBK

首先,在您的个人计算机上创建 vpn 连接。它将存储在%userprofile%\AppData\Roaming\Microsoft\Network\Connections\PBK

then open the rasphone.pbk file with a text editor ( for ex. Notepad++) and copy your connect informations. But don't forget. Before this step, you have to be completed to your connection settings. And then, copy your connection informations from rasphone.pbk file and paste into the script (to rasphone string). Tip: rasphone.pbk file contains just settings. No username and passwords...

然后使用文本编辑器(例如 Notepad++)打开 rasphone.pbk 文件并复制您的连接信息。但不要忘记。在此步骤之前,您必须完成对您的连接设置。然后,从 rasphone.pbk 文件复制您的连接信息并粘贴到脚本中(到 rasphone 字符串)。提示:rasphone.pbk 文件只包含设置。没有用户名和密码...

In my code, I create a connection with these settings;
Connection Name : RAS -> At first Line [RAS]
Connection Ip : ras.ugurturhal.com
VPN Type: PPTP, supported CHAP and MS-CHAP v2 protocols

在我的代码中,我使用这些设置创建了一个连接;
连接名称:RAS -> At first Line [RAS]
连接 Ip:ras.ugurturhal.com
VPN 类型:PPTP,支持 CHAP 和 MS-CHAP v2 协议

The code is check your VPN connections. If you have a connection which name is RAS or IP adress is ras.ugurturhal.com, code changes nothing and warn to you. If you have a lot of VPN connections and none of them have same settings (on above), it will also "ADD" this VPN. And the last thing, if you have no VPN connection, it will be create a new one.

代码是检查您的 VPN 连接。如果您有一个名称为 RAS 或 IP 地址为 ras.ugurturhal.com 的连接,则代码不会更改任何内容并向您发出警告。如果您有很多 VPN 连接并且它们都没有相同的设置(在上面),它也会“添加”这个 VPN。最后一件事,如果您没有 VPN 连接,它将创建一个新的连接。

Note: You can't see the VPN connection which you created by C# on your networks. But it will be work. And after the restart to computer, will see it on your networks :)

注意:你在你的网络上看不到你用 C# 创建的 VPN 连接。但它会起作用。重新启动计算机后,将在您的网络上看到它:)

I hope it will helps to somebody..

我希望它会对某人有所帮助..

And you can download of my project file from my Web Host..

您可以从我的Web Host下载我的项目文件。

Regards.

问候。

PS : I tried to add all of my codes to here but very new in stackoverflow. Please forgive me. You can download the sample ;)

PS:我尝试将所有代码添加到此处,但在stackoverflow中非常新。请原谅我。您可以下载示例;)

PS2 : Or you can check it from GitHub. Thanks @axrwkr

PS2:或者你可以从GitHub 上查看。谢谢@axrwkr

回答by Ricardo Villamil

Look up the RAS API (Remote Access Service), it's a win32 dll, but you can call it from C#. Here are some links that may help you get started:

查找 RAS API(远程访问服务),它是一个 win32 dll,但您可以从 C# 调用它。以下是一些可以帮助您入门的链接:

回答by Yuri Perphilev

On Windows ras phonebook is stored in a simple ini file that could be found at

在 Windows 上,ras 电话簿存储在一个简单的 ini 文件中,可以在以下位置找到

C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk

C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\rasphone.pbk

You could prepare such a file at your computer and then deploy this file to customers by simple copying it to the specified path (or append it's content to the existing one). To display new connection in the Network Connections panel, just refreshed explorer's view (via F5key), no explorer restart is even needed.

您可以在您的计算机上准备这样一个文件,然后通过简单地将其复制到指定路径(或将其内容附加到现有路径)来将该文件部署给客户。要在“网络连接”面板中显示新连接,只需刷新资源管理器的视图(通过F5键),甚至不需要重新启动资源管理器。

回答by Frank Hauptle

I know it's not the "approved" method. but I've been doing this for years without issue.

我知道这不是“批准”的方法。但我多年来一直这样做没有问题。

setup the vpn on your own computer, make sure there is only this one VPN setup in there or you'll deploy all of them to your staff. Find the rasphone.pbk file on your PC, rename it to myVPN.pbk (or whatever you want) and use Altiris or whatever to deploy it to everyones desktop.

在您自己的计算机上设置 vpn,确保其中只有这一个 VPN 设置,否则您会将它们全部部署给您的员工。在您的 PC 上找到 rasphone.pbk 文件,将其重命名为 myVPN.pbk(或您想要的任何名称),然后使用 Altiris 或其他任何方式将其部署到每个人的桌面上。

When they click on it, it opens up and works fine. As an added benefit put it online and tell your staff to right click on the link and save it to their desktop and dbl click on it. our uni has been doing it this way since I first found it worked most of 10 years ago.

当他们点击它时,它会打开并且工作正常。作为额外的好处,将它放到网上并告诉您的员工右键单击该链接并将其保存到他们的桌面,然后 dbl 单击它。自从我在 10 年前的大部分时间里第一次发现它有效以来,我们的大学一直在这样做。

回答by Akram Bouktab

My be the CMAKwill help (CONNECTION MANAGER ADMINISTRATOR KIT)

我的CMAK会有所帮助(连接管理器管理员套件)

回答by caglarozdag

You need a batch script to connect to a VPN that is set up on the base machine. I had a similar problem to deal with a while ago and I found thisand thislink to be helpful. I have used this solution (first link) in the past so it works but I don't have the batch script anymore.

您需要一个批处理脚本来连接到在基础机器上设置的 VPN。不久前我遇到了类似的问题,我发现这个这个链接很有帮助。我过去使用过这个解决方案(第一个链接),所以它可以工作,但我没有批处理脚本了。

1.) Include rasphone.pbk in your script (under C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\ in XP. under C:\Users\USERNAME\AppData\Roaming\Microsoft\Network\Connections\Pbk in Vista)

1.) 在您的脚本中包含 rasphone.pbk(在 XP 下的 C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\ 下。在 C:\Users\USERNAME\AppData\Roaming\Microsoft\ Vista中的网络\连接\Pbk)

2.) Set working directory to a variable name so you can return to it

2.) 将工作目录设置为变量名,以便您可以返回到它

3.) Move to the directory where the phonebook is stored using variable names based on the user profile

3.) 使用基于用户配置文件的变量名称移动到存储电话簿的目录

4.) Test that the phone book is there, if it is, rename it to rasphone.pbk.bk, then xcopy from the script location (as saved in variable) the new phone book to the new place.

4.) 测试电话簿是否存在,如果存在,将其重命名为 rasphone.pbk.bk,然后将新电话簿从脚本位置(保存在变量中)xcopy 到新位置。

5.) Connect.

5.) 连接。

6.) When ready, disconnect and overwrite the phone book with your settings in with the original file if it is there, else simply delete the phone book.

6.) 准备好后,断开并用您的设置覆盖电话簿,如果有原始文件,否则只需删除电话簿。