1
'---------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET Framework SDK Code Samples.
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
6 'This source code is intended only as a supplement to Microsoft
7 'Development Tools and/or on-line documentation. See these other
8 'materials for detailed information regarding Microsoft code samples.
10 'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 '---------------------------------------------------------------------
18 Imports System
.Net
.Sockets
19 Imports System
.Net
.Security
20 Imports System
.Security
.Authentication
21 Imports System
.Security
.Cryptography
.X509Certificates
26 Connect("localhost", "howdy")
29 Private Sub Connect(ByVal server
As String, ByVal message
As String)
30 Dim sslStream
As SslStream
= Nothing
33 ' Note, for this client to work you need to have a TcpServer
34 ' connected to the same address as specified by the server,
36 Dim port
As Integer = 13000
37 Dim client
As New TcpClient(server
, port
)
39 ' Translate the passed message into ASCII and store it as a
41 Dim data() As Byte = System
.Text
.Encoding
.ASCII
.GetBytes(message
)
43 Dim callback
As RemoteCertificateValidationCallback = _
44 AddressOf OnCertificateValidation
46 ' Get a client stream for reading and writing.
47 ' Wrap it in an SslStream.
48 sslStream
= New SslStream(client
.GetStream(), False, callback
)
49 sslStream
.AuthenticateAsClient("localhost")
51 If sslStream
.IsAuthenticated
Then
53 "IsAuthenticated: {0}", _
54 sslStream
.IsAuthenticated
)
56 "IsMutuallyAuthenticated: {0}", _
57 sslStream
.IsMutuallyAuthenticated
)
60 sslStream
.IsEncrypted
)
69 ' Send the message to the connected TcpServer.
70 sslStream
.Write(data
, 0, data
.Length
)
72 Console
.WriteLine("Sent: {0}", message
)
74 ' Receive the TcpServer.response.
75 ' Buffer to store the response bytes.
78 ' String to store the response ASCII representation.
79 Dim responseData
As String = String.Empty
81 ' Read the first batch of the TcpServer response bytes.
82 Dim bytes
As Integer = sslStream
.Read(data
, 0, data
.Length
)
84 System
.Text
.Encoding
.ASCII
.GetString(data
, 0, bytes
)
85 Console
.WriteLine("Received: {0}", responseData
)
86 Catch ex
As AuthenticationException
87 Console
.WriteLine(ex
.Message
)
88 Catch ex
As SocketException
89 Console
.WriteLine(ex
.Message
)
90 Catch ex
As IOException
91 Console
.WriteLine(ex
.Message
)
93 If sslStream IsNot
Nothing Then
98 Console
.WriteLine(Environment
.NewLine
& "Press Enter to continue...")
102 Function OnCertificateValidation( _
103 ByVal sender
As Object, _
104 ByVal certificate
As X509Certificate
, _
105 ByVal chain
As X509Chain
, _
106 ByVal sslPolicyErrors
As SslPolicyErrors
) _
109 ' This sample returns true always so that you can see how
110 ' it works. You should change logic in the validator to only
111 ' return true if you've inspected the certificate and approve it.