added samples
[windows-sources.git] / sdk / samples / FrameworkSamples / NCL / SecureStreams / vb / negotiateclient / negotiateclient.vb
blob1d0f2fe7c200b8f589f4aa239d067297d5616dba
1 '---------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET Framework SDK Code Samples.
3 '
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
5 '
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.
9 '
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
13 'PARTICULAR PURPOSE.
14 '---------------------------------------------------------------------
16 Imports System.IO
17 Imports System.Net
18 Imports System.Net.Sockets
19 Imports System.Net.Security
20 Imports System.Security.Authentication
21 Imports System.Security.Principal
23 Module NegotiateClient
25 Sub Main()
26 ' You should substitute in the SPN of the server you want to
27 ' authenticate to using Kerberos. Otherwise, NTLM authentication
28 ' will be used.
29 Connect("localhost", "howdy", "domain\\user")
30 End Sub
32 Private Sub Connect( _
33 ByVal server As String, ByVal message As String, _
34 ByVal servicePrincipalName As String)
35 Dim negotiateStream As NegotiateStream = Nothing
36 Try
37 ' Create a TcpClient.
38 ' Note, for this client to work you need to have a TcpServer
39 ' connected to the same address as specified by the server,
40 ' port combination.
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
45 ' Byte array.
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
64 Console.WriteLine( _
65 "IsAuthenticated: {0}", _
66 negotiateStream.IsAuthenticated)
67 Console.WriteLine( _
68 "IsMutuallyAuthenticated: {0}", _
69 negotiateStream.IsMutuallyAuthenticated)
70 Console.WriteLine( _
71 "IsEncrypted: {0}", _
72 negotiateStream.IsEncrypted)
73 Console.WriteLine( _
74 "IsSigned: {0}", _
75 negotiateStream.IsSigned)
76 Console.WriteLine( _
77 "IsServer: {0}", _
78 negotiateStream.IsServer)
79 End If
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.
88 ReDim data(256)
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)
95 responseData = _
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)
104 Finally
105 If negotiateStream IsNot Nothing Then
106 negotiateStream.Close()
107 End If
108 End Try
110 Console.WriteLine(Environment.NewLine & "Press Enter to continue...")
111 Console.Read()
112 End Sub
114 End Module