Search code examples
c#.netpinvokeras

Can't dial with DotRas in Windows Server 2012 R2


i'm totally new to DotRas so please be patient.. I have a windows server 2012 R2 with "Route and remote access" configured. In this simple configuration there is a dialer connection to a vpn as you can see in the first picture..

enter image description here

So, if i click connect.. everything works fine and the status of the network interface changes from disconnected to connected. Going to the event viewer this is what i got:

Events from RASCLIENT sourcelog:

Event 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.

Event 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.

Event 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.

Event 4:

CoId={A52088DC-5358-44D6-8B77-DA49516C3FBD}: The link to the Remote Access Server has been established by user SYSTEM.

Event 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.

Now.. my goal is to get connected from a windows service

So here is my code (the essential part):

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, no matter what... going to the event viewer looking for the RasClient event source, as expected i got 5 events logged. 1,2,3 and 4 are equal to the ones generated by the manual connection unfortunately the last (5) is:

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.

Please consider that if you think at some wrong credentials or else.. i did a try with no credentials at all and as expected in that case i wasn't able to get event 4 Any ideas?


Solution

  • First thing first! This is not the answer to my question but... I think it could be helpful for anyone who's experienced the same trouble.

    After a bunch of tries and even some attempts to debug using DotRas source code i didn't get any step forward, besides i'm not sure this can actually be done. I said that because trying with "rasdial" command from an administrator command prompt i've got the same result: failed with error code 720. That said i've got my solution using powershell.

    There's 3 powerful cmdlets which are all i was looking for

    - Get-VpnS2SInterface - Connect-VpnS2SInterface - Disconnect-VpnS2SInterface

    So i just set 3 simple methods and now everything works like a charm

    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());
        }
    }