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
.Principal
23 Module NegotiateClient
26 ' You should substitute in the SPN of the server you want to
27 ' authenticate to using Kerberos. Otherwise, NTLM authentication
29 Connect("localhost", "howdy", "domain\\user")
32 Private Sub Connect( _
33 ByVal server
As String, ByVal message
As String, _
34 ByVal servicePrincipalName
As String)
35 Dim negotiateStream
As NegotiateStream
= Nothing
38 ' Note, for this client to work you need to have a TcpServer
39 ' connected to the same address as specified by the server,
41 Dim port
As Integer = 13000
42 Dim client
As TcpClient
= New TcpClient(server
, port
)
44 ' Translate the passed message into ASCII and store it as a
46 Dim data() As Byte = System
.Text
.Encoding
.ASCII
.GetBytes(message
)
48 ' Get a client stream for reading and writing.
49 ' Wrap it in a NegotiateStream.
50 negotiateStream
= New NegotiateStream(client
.GetStream())
52 ' This example uses the SPN which is required for Kerberos.
53 ' If you don't know your service principal name, you can do
54 ' NTLM authentication by commenting out the line below
55 negotiateStream
.AuthenticateAsClient( _
56 CredentialCache
.DefaultNetworkCredentials
, _
57 servicePrincipalName
, _
58 ProtectionLevel
.EncryptAndSign
, _
59 TokenImpersonationLevel
.Impersonation
)
60 ' And then uncomment this line
61 ' authenticatedStream.AuthenticateAsClient();
63 If negotiateStream
.IsAuthenticated
Then
65 "IsAuthenticated: {0}", _
66 negotiateStream
.IsAuthenticated
)
68 "IsMutuallyAuthenticated: {0}", _
69 negotiateStream
.IsMutuallyAuthenticated
)
72 negotiateStream
.IsEncrypted
)
75 negotiateStream
.IsSigned
)
78 negotiateStream
.IsServer
)
81 ' Send the message to the connected TcpServer.
82 negotiateStream
.Write(data
, 0, data
.Length
)
84 Console
.WriteLine("Sent: {0}", message
)
86 ' Receive the TcpServer.response:
87 ' Buffer to store the response bytes.
90 ' String to store the response ASCII representation.
91 Dim responseData
As String = String.Empty
93 ' Read the first batch of the TcpServer response bytes.
94 Dim bytes
As Integer = negotiateStream
.Read(data
, 0, data
.Length
)
96 System
.Text
.Encoding
.ASCII
.GetString(data
, 0, bytes
)
97 Console
.WriteLine("Received: {0}", responseData
)
98 Catch ex
As AuthenticationException
99 Console
.WriteLine(ex
.Message
)
100 Catch ex
As SocketException
101 Console
.WriteLine(ex
.Message
)
102 Catch ex
As IOException
103 Console
.WriteLine(ex
.Message
)
105 If negotiateStream IsNot
Nothing Then
106 negotiateStream
.Close()
110 Console
.WriteLine(Environment
.NewLine
& "Press Enter to continue...")