我对DotRas完全陌生,所以请耐心等待。我有一个带有"路由和远程访问的Windows服务器2012 R2"配置。在这个简单的配置中,有一个拨号连接到vpn,你可以在第一张图片中看到..
所以,如果我点击连接..一切正常,网络接口的状态从断开连接变为连接。 转到事件查看器这是我得到的:
来自RASCLIENT sourcelog的事件:
活动1:
CoId={A52088DC-5358-44D6-8B77-DA49516C3FBD}: The user SYSTEM has started dialing a VPN connection using a all-user connection profile named VpnAtlanta02. The connection settings are:
Dial-in User = c******s
VpnStrategy = PPTP
DataEncryption = Require
PrerequisiteEntry =
AutoLogon = No
UseRasCredentials = Yes
Authentication Type = MS-CHAPv2
Ipv4DefaultGateway = No
Ipv4AddressAssignment = By Server
Ipv4DNSServerAssignment = By Server
Ipv6DefaultGateway = Yes
Ipv6AddressAssignment = By Server
Ipv6DNSServerAssignment = By Server
IpDnsFlags =
IpNBTEnabled = No
UseFlags = Private Connection
ConnectOnWinlogon = No.
活动2:
CoId={A52088DC-5358-44D6-8B77-DA49516C3FBD}: The user SYSTEM is trying to establish a link to the Remote Access Server for the connection named VpnAtlanta02 using the following device:
Server address/Phone Number = ***.***.***.***
Device = WAN Miniport (PPTP)
Port = VPN3-4
MediaType = VPN.
活动3:
CoId={A52088DC-5358-44D6-8B77-DA49516C3FBD}: The user SYSTEM has successfully established a link to the Remote Access Server using the following device:
Server address/Phone Number = ***.***.***.***
Device = WAN Miniport (PPTP)
Port = VPN3-4
MediaType = VPN.
活动4:
CoId={A52088DC-5358-44D6-8B77-DA49516C3FBD}: The link to the Remote Access Server has been established by user SYSTEM.
活动5:
CoId={A52088DC-5358-44D6-8B77-DA49516C3FBD}: The user SYSTEM has dialed a connection named VpnAtlanta02 to the Remote Access Server which has successfully connected. The connection parameters are:
TunnelIpAddress = 172.20.0.19
TunnelIpv6Address = None
Dial-in User = c******s.
现在..我的目标是从Windows服务
连接所以这是我的代码(必不可少的部分):
Dialer = new RasDialer();
Dialer.PhoneBookPath = "C:\\Windows\\System32\\ras\\Router.pbk";
Dialer.Timeout = 20 * 1000;
Dialer.HangUpPollingInterval = 20 * 1000;
Dialer.AllowUseStoredCredentials = false;
Dialer.AutoUpdateCredentials = RasUpdateCredential.None;
Dialer.EntryName = "VpnAtlanta02";
Dialer.Credentials = new System.Net.NetworkCredential("c******s", "*********");
Watcher = new RasConnectionWatcher();
Watcher.EnableRaisingEvents = true;
Watcher.Connected += Watcher_Connected;
Watcher.Disconnected += Watcher_Disconnected;
InfoLog("Begin connection");
Watcher.Handle = Dialer.Dial();
private void Watcher_Disconnected(object sender, RasConnectionEventArgs e)
{
InfoLog(e.Connection.EntryName + " is disconnected");
}
private void Watcher_Connected(object sender, RasConnectionEventArgs e)
{
InfoLog(e.Connection.EntryName + " is connected");
}
Sofar,无论如何......前往事件查看器寻找RasClient事件源,正如预期的那样我记录了5个事件。 1,2,3和4等于手动连接生成的,不幸的是,最后一个(5)是:
CoId={E2814072-13C7-44CF-998A-A1160FDC86E3}: The user SYSTEM dialed a connection named VpnAtlanta02 which has failed. The error code returned on failure is 720.
请考虑一下,如果您认为某些错误的凭据或其他..我尝试过根本没有凭据,并且正如预期的那样,我无法获得活动4 有什么想法吗?
答案 0 :(得分:0)
首先要做的事情!对于我的问题,这不是答案,但是......我认为对于遇到同样问题的人来说,这可能会有所帮助。
经过一系列尝试甚至一些尝试使用DotRas源代码进行调试后,我没有向前迈进任何一步,除此之外我不确定这实际上是否可以完成。我说,因为从管理员命令提示符尝试使用“rasdial”命令我得到了相同的结果:失败了,错误代码为720.这就是说我已经使用PowerShell获得了我的解决方案。
我正在寻找的是3个强大的cmdlet
- Get-VpnS2SInterface - Connect-VpnS2SInterface - Disconnect-VpnS2SInterface
所以我只设置了3个简单的方法,现在一切都像魅力
public Boolean IsConnected()
{
Boolean retVal = false;
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand("Get-VpnS2SInterface");
ps.AddParameter("Name", "VpnAtlanta02");
foreach (PSObject result in ps.Invoke())
{
retVal = ("" + result.Members["ConnectionState"].Value).ToLower() == "connected";
}
}
return retVal;
}
public void Connect()
{
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand("Connect-VpnS2SInterface");
ps.AddParameter("Name", "VpnAtlanta02");
ps.AddParameter("PassThru");
foreach (PSObject result in ps.Invoke())
{
String destination = "";
foreach (String s in result.Members["Destination"].Value as String[])
{
destination += "{" + s + "}";
}
Service1.InfoLog("Destination=" + destination + "\r\n" +
"ConnectionState=" + result.Members["ConnectionState"].Value + "\r\n");
}
}
}
public void Disconnect()
{
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand("Disconnect-VpnS2SInterface");
ps.AddParameter("Name", "VpnAtlanta02");
ps.AddParameter("Force");
ps.Invoke();
Service1.InfoLog("Nic: " + "VpnAtlanta02" + " is connected: " + IsConnected());
}
}