DotRas 1.3 dial created Vpn entry error from application but not from Windows

750 Views Asked by At

I'm using dotras to create a vpn connection. When I try to connect to the vpn using the dotras dial async method it doesn't work:

"Error 718: The connection was terminated because the remote computer did not respond in a timely manner".

So I tried connecting to the vpn connection i've just created via the windows interface and it works just fine. Strangely enough, I then tried to connect again via the dotras method and it worked!

So, if I try to connect first via dotras I can't, but if I connect first via windows, disconnect, and then connect again via dotras it works.

I'm using Windows 10 pro and Windows 2012 server and the version of DotRas is 1.3

Here is my code:

     RasPhoneBook _allUsersPhoneBook;
            public RasPhoneBook allUsersPhoneBook
            {
                get
                { 
                    _allUsersPhoneBook = new RasPhoneBook();
                    _allUsersPhoneBook.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers));

                    return _allUsersPhoneBook;
                }
                set
                {
                    _allUsersPhoneBook = value;
                }
            }
         private RasDialer _dialer;
                public RasDialer dialer
                {
                    get
                    {
                       if (_dialer== null)
                        {
                            _dialer = new RasDialer();
                            _dialer.Error += new EventHandler<ErrorEventArgs>(Dialer_Error);
                            _dialer.StateChanged += new EventHandler<StateChangedEventArgs>(Dialer_StateChanged);                    
                            _dialer.EntryName = connectionName;
                            _dialer.PhoneBookPath = allUsersPhoneBook.Path;                        
                        }

                        return _dialer;
                    }
                    set
                    {
                        _dialer = value;
                    }
                }

//Here I create the connection entry or if already exists update its properties
         public void CreateOrUpdate()
                {
                    using (dialer)
                    {
                        using (var allUsersPhoneBookLocal = new RasPhoneBook())
                        {
                            allUsersPhoneBookLocal.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers));                   
                            if (allUsersPhoneBook.Entries.Contains(connectionName))
                            {
                                allUsersPhoneBookLocal.Entries[connectionName].PhoneNumber = serverAddress;
                                allUsersPhoneBookLocal.Entries[connectionName].VpnStrategy = RasVpnStrategy;
                                allUsersPhoneBookLocal.Entries[connectionName].Device = RasDevice;
                                allUsersPhoneBookLocal.Entries[connectionName].Options.RemoteDefaultGateway = false;
                                allUsersPhoneBookLocal.Entries[connectionName].Options.IPv6RemoteDefaultGateway = false;
                                allUsersPhoneBookLocal.Entries[connectionName].Options.CacheCredentials = true;
                                allUsersPhoneBookLocal.Entries[connectionName].UpdateCredentials(new System.Net.NetworkCredential(userName, passWord));

                                allUsersPhoneBookLocal.Entries[connectionName].Update();

                            }
                            else
                            {   
                                RasEntry entry = RasEntry.CreateVpnEntry(connectionName, serverAddress, RasVpnStrategy.IkeV2Only,
                                RasDevice);


                                entry.EncryptionType = RasEncryptionType.Optional;                                            

                                entry.Options.IPv6RemoteDefaultGateway = false;
                                entry.Options.RemoteDefaultGateway = false;
                                entry.Options.CacheCredentials = true;


                                /*
                                dialer.EntryName = connectionName;
                                dialer.Credentials = new System.Net.NetworkCredential(userName, ConvertToSecureString(passWord));                       

                                dialer.PhoneBookPath = allUserPhoneBookPath;
                                */


                                allUsersPhoneBook.Entries.Add(entry);
                                entry.UpdateCredentials(new System.Net.NetworkCredential(userName, ConvertToSecureString(passWord)));
                                // 26 means eap-mschapv2 username/password
                                entry.UpdateCredentials(new System.Net.NetworkCredential(userName, ConvertToSecureString(passWord)));
                               // entry.Options.RequireEap = true;
                                entry.CustomAuthKey = 26;
                                entry.Update();
                                entry.VpnStrategy = RasVpnStrategy;
                                entry.Update();

                            }
                        }
                    }
                }

//here I dial the connection

          public RasHandle Dial()
            {
                RasHandle handle = null;
                try
                {
                    using (allUsersPhoneBook)
                    {
                        if (allUsersPhoneBook.Entries.Contains(connectionName))
                        {
                            handle = dialer.DialAsync();                           
                        }
                    }
                    return handle;
                }
                catch (Exception)
                {
                    throw;
                }
            }
0

There are 0 best solutions below