如何在不始终提示输入密码的情况下使用rasphone.exe获得VPN连接

时间:2019-03-04 10:42:09

标签: vb.net vpn

我有一个使用rasphone.exe界面连接Windows VPN(不是OpenVPN)的应用。我可以成功建立连接,但是每次都会提示输入密码。有没有一种方法可以使接口记住密码,以便在连接丢失的情况下可以通过编程方式自动重新建立连接?作为一个提示,当我启动rasphone.exe进程时,尝试向其传递多个参数时出现异常。我可以成功传递的唯一参数是条目名称,我无法添加-d,-h或-f之类的参数。

这是我的代码:

Imports System.Linq
Imports System.Net.NetworkInformation

Public Class clsVPN

    Public Delegate Sub delPing()
    Public Delegate Sub delConnect()
    Public Delegate Sub delIdle()
    Public Delegate Sub delDisconnect()
    Public Delegate Sub delStatus(blnConnected As Boolean)

    Public Event Ping As delPing
    Public Event Con As delConnect
    Public Event Discon As delDisconnect
    Public Event Idle As delIdle
    Public Event StatusChanged As delStatus

    Public strRASPhone As String = "C:\WINDOWS\system32\rasphone.exe"
    Public strIPAddress As String = ""
    Public strVPNCon As String = ""

    Public blnConnected As Boolean = False

    Dim file As String = "C : \Users\Tom\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk"


    Protected Sub OnStatusChanged(blnConnected As Boolean)

        RaiseEvent StatusChanged(blnConnected)

    End Sub

    Protected Sub OnDisconnect()

        RaiseEvent Discon()

    End Sub

    Protected Sub OnPing()

        RaiseEvent Ping()

    End Sub

    Protected Sub OnIdle()

        RaiseEvent Idle()

    End Sub

    Protected Sub OnConnect()

        RaiseEvent Con()

    End Sub

    Public ReadOnly Property Connected() As Boolean

        Get

            Return blnConnected

        End Get

    End Property


    Public Property ConName() As String

        Get

            Return strVPNCon

        End Get

        Set(strValue As String)

            strVPNCon = strValue

        End Set

    End Property

    Public Function Test() As Boolean

        Dim blnSucceed As Boolean = False

        OnPing()

        Dim p As New Ping()

        If p.Send(strIPAddress).Status = IPStatus.Success Then

            blnSucceed = True

        Else

            blnSucceed = False

        End If

        p = Nothing

        If blnSucceed <> blnConnected Then

            blnConnected = blnSucceed

            OnStatusChanged(blnConnected)

        End If

        OnIdle()

        Return blnSucceed

    End Function

    Public Function Connect() As Boolean

        Dim blnSucceed As Boolean = False
        Dim optionstr As String = "-f " & file & " -d "

        OnConnect()

        'MessageBox.Show("strVPNCon = " )

        'Process.Start(strRASPhone, Convert.ToString(" -f ") & file & Convert.ToString(" -d ") _
        ' & strVPNCon)
        optionstr = ""

        Dim wait As Boolean = True

        ProcessExec(strRASPhone, optionstr & strVPNCon, wait)

        Application.DoEvents()

        System.Threading.Thread.Sleep(5000)

        Application.DoEvents()

        blnSucceed = True

        OnIdle()

        Return blnSucceed

    End Function

    Public Function Disconnect() As Boolean

        Dim blnSucceed As Boolean = False
        Dim optionstr As String = "-h "

        OnDisconnect()

        optionstr = ""

        Dim wait As Boolean = True

        ProcessExec(strRASPhone, optionstr & strVPNCon, wait)

        Application.DoEvents()

        System.Threading.Thread.Sleep(8000)

        Application.DoEvents()

        blnSucceed = True

        OnIdle()

        Return blnSucceed

    End Function

    Public Function CheckConnection() As Boolean

        Dim niVPN As NetworkInterface() =
         NetworkInterface.GetAllNetworkInterfaces

        Dim blnExist As Boolean =
         niVPN.AsEnumerable().Any(Function(x) x.Name = ConName)

        If blnExist Then

            'MessageBox.Show("VPN Exists")

        Else

            ' MessageBox.Show("VPN Does Not Exist")

        End If
        Return blnExist

    End Function

    Public Sub ProcessExec(processarg As String, param As String, wait As Boolean)

        ' Start the child process.
        Dim p As New ProcessStartInfo
        ' Redirect the output stream of the child process.
        p.FileName = processarg
        p.Arguments = param
        p.UseShellExecute = True
        p.WindowStyle = ProcessWindowStyle.Normal
        Dim proc As Process = Process.Start(p)
        ' Do Not wait for the child process to exit before
        ' reading to the end of its redirected stream.
        If wait = True Then
            proc.WaitForExit()
        End If

    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

我知道这是一年多以前问的。

我认为所描述的问题不是由vb代码引起的。该问题可能是由于文件rasphone.pbk引起的。它通常包含在%APPDATA%\Microsoft\Network\Connections\Pbk中。可以使用文本编辑器读取此文件。 PreviewUserPw之后有一个选项[YOURVPNNAME]。值1会在拨入之前产生提示。

将此行更改为

PreviewUserPw=0

会有所帮助。