DotRas 1.3拨号从应用程序创建Vpn条目错误,但不从Windows创建

时间:2017-11-10 15:09:15

标签: vpn dotras

我正在使用dotras创建一个vpn连接。当我尝试使用dotras拨号异步方法连接到vpn时,它不起作用:

  

“错误718:连接已终止,因为远程计算机未及时响应”。

所以我尝试连接到我刚刚通过windows界面创建的vpn连接,它工作得很好。奇怪的是,我接着尝试通过dotras方法再次连接并且它有效!

所以,如果我尝试首先通过dotras连接我不能,但是如果我先通过windows连接,断开连接,再通过dotras再次连接就可以了。

我正在使用Windows 10专业版和Windows 2012服务器,而DotRas的版本是1.3

这是我的代码:

     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 个答案:

没有答案