VPN Connection mit DotRas, Erstellen/Verbinden/Trennen/Löschen

  • VB.NET

    VPN Connection mit DotRas, Erstellen/Verbinden/Trennen/Löschen

    Hallo Zusammen,

    Habe hier gesucht, leider nichts gefunden. Selber als "Noob" probiert, geschafft xD
    Deshalb Poste ich hier das DotRas Beispiel vom Example:


    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Net
    2. Imports DotRas
    3. Public Class MainForm
    4. Public Const EntryName As String = "VPN Connection"
    5. Private connectionHandle As RasHandle
    6. Private Sub CreateEntryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateEntryButton.Click
    7. ' This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created.
    8. Me.AllUsersPhoneBook.Open()
    9. ' Create the entry that will be used by the dialer to dial the connection. Entries can be created manually, however the static methods on
    10. ' the RasEntry class shown below contain default information matching that what is set by Windows for each platform.
    11. Dim entry As RasEntry = RasEntry.CreateVpnEntry(EntryName, IPAddress.Loopback.ToString(), RasVpnStrategy.Default, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn))
    12. ' Add the new entry to the phone book.
    13. Me.AllUsersPhoneBook.Entries.Add(entry)
    14. End Sub
    15. Private Sub DialButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DialButton.Click
    16. Me.StatusTextBox.Clear()
    17. ' This button will be used to dial the connection.
    18. Me.Dialer.EntryName = EntryName
    19. Me.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers)
    20. Try
    21. ' Set the credentials the dialer should use.
    22. Me.Dialer.Credentials = New NetworkCredential(userBox.Text, passBox.Text)
    23. ' NOTE: The entry MUST be in the phone book before the connection can be dialed.
    24. ' Begin dialing the connection; this will raise events from the dialer instance.
    25. Me.Dialer.DialAsync()
    26. ' Enable the disconnect button for use later.
    27. Me.DisconnectButton.Enabled = True
    28. Catch ex As Exception
    29. Me.StatusTextBox.AppendText(ex.ToString())
    30. End Try
    31. End Sub
    32. Private Sub Dialer_StateChanged(ByVal sender As System.Object, ByVal e As DotRas.StateChangedEventArgs) Handles Dialer.StateChanged
    33. Me.StatusTextBox.AppendText(e.State.ToString() + Chr(13) + Chr(10))
    34. End Sub
    35. Private Sub Dialer_DialCompleted(ByVal sender As System.Object, ByVal e As DotRas.DialCompletedEventArgs) Handles Dialer.DialCompleted
    36. If (e.Cancelled) Then
    37. Me.StatusTextBox.AppendText("Cancelled!")
    38. ElseIf (e.TimedOut) Then
    39. Me.StatusTextBox.AppendText("Connection attempt timed out!")
    40. ElseIf (e.Error IsNot Nothing) Then
    41. Me.StatusTextBox.AppendText(e.Error.ToString())
    42. ElseIf (e.Connected) Then
    43. Me.StatusTextBox.AppendText("Connection successful!")
    44. End If
    45. If (Not e.Connected) Then
    46. Me.DisconnectButton.Enabled = False
    47. End If
    48. End Sub
    49. Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
    50. If (Me.Dialer.IsBusy) Then
    51. Me.Dialer.DialAsyncCancel()
    52. Else
    53. ' The connection attempt has completed, attempt to find the connection in the active connections.
    54. Dim connection As RasConnection = RasConnection.GetActiveConnectionByHandle(Me.connectionHandle)
    55. If (connection IsNot Nothing) Then
    56. ' The connection has been found, disconnect it.
    57. connection.HangUp()
    58. End If
    59. End If
    60. End Sub
    61. End Class




    Eigene IP Eintragen: Original

    VB.NET-Quellcode

    1. IPAdress.Loopback.ToString()

    ändern in:

    VB.NET-Quellcode

    1. "IP-Adresse"


    und zum den User und das Passwort zu übermitteln:

    VB.NET-Quellcode

    1. Me.Dialer.Credentials = New NetworkCredential(userBox.Text, passBox.Text)


    http://dotras.codeplex.com

    *Topic verschoben*

    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „Marcus Gräfe“ ()