我正在使用 dotras 创建一个 vpn 连接。当我尝试使用 dotras dial async 方法连接到 vpn 时,它不起作用:
“错误 718:连接被终止,因为远程计算机没有及时响应”。
所以我尝试通过windows界面连接到我刚刚创建的vpn连接,它工作得很好。奇怪的是,然后我尝试通过 dotras 方法再次连接,它成功了!
所以,如果我尝试先通过 dotras 连接,我不能,但如果我先通过 windows 连接,断开连接,然后通过 dotras 再次连接它可以工作。
我使用的是 Windows 10 pro 和 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;
}
}