无法在 Windows Server 2012 R2 中使用 DotRas 拨号

Can't dial with DotRas in Windows Server 2012 R2

我是 DotRas 的新手,所以请耐心等待。我有一个 windows 服务器 2012 R2 配置了 "Route and remote access"。在这个简单的配置中,有一个拨号器连接到 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");
}

到目前为止,无论如何...转到事件查看器寻找 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 有什么想法吗?

第一件事第一!这不是我问题的答案,但是...我认为这对遇到同样问题的任何人都有帮助。

经过多次尝试,甚至尝试使用 DotRas 源代码进行调试后,我没有取得任何进展,而且我不确定这是否真的可以完成。我说因为在管理员命令提示符下尝试使用 "rasdial" 命令我得到了相同的结果:失败,错误代码 720。也就是说我已经使用 powershell 得到了我的解决方案。

我一直在寻找 3 个功能强大的 cmdlet

-获取-VpnS2SInterface - 连接-VpnS2SInterface - 断开连接-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());
    }
}