internal void SendKerberos(Object threadContext) { try { if (GetPackets()) { return; } if (_SocketTCPClient.Connected) { try { _AuthStream.BeginWrite(byteData._SendData, 0, byteData._SendData.Length, mySendKerberosCallback, _AuthStream); } catch { Stop(); } } else { Stop(); } } catch { Stop(); } }
/// <summary> /// Extends BeginWrite so that when a state object is not needed, null does not need to be passed. /// <example> /// negotiatestream.BeginWrite(buffer, offset, count, asyncCallback); /// </example> /// </summary> public static IAsyncResult BeginWrite(this NegotiateStream negotiatestream, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback asyncCallback) { if (negotiatestream == null) { throw new ArgumentNullException("negotiatestream"); } return(negotiatestream.BeginWrite(buffer, offset, count, asyncCallback, null)); }
public static void Main(String[] args) { //<snippet2> //<snippet1> // Establish the remote endpoint for the socket. // For this example, use the local machine. IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost"); IPAddress ipAddress = ipHostInfo.AddressList[0]; // Client and server use port 11000. IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. client = new TcpClient(); // Connect the socket to the remote endpoint. client.Connect(remoteEP); Console.WriteLine("Client connected to {0}.", remoteEP.ToString()); // Ensure the client does not close when there is // still data to be sent to the server. client.LingerState = (new LingerOption(true, 0)); //<snippet3> // Request authentication. NetworkStream clientStream = client.GetStream(); NegotiateStream authStream = new NegotiateStream(clientStream, false); //</snippet1> // Pass the NegotiateStream as the AsyncState object // so that it is available to the callback delegate. IAsyncResult ar = authStream.BeginAuthenticateAsClient( new AsyncCallback(EndAuthenticateCallback), authStream ); //</snippet2> Console.WriteLine("Client waiting for authentication..."); // Wait until the result is available. ar.AsyncWaitHandle.WaitOne(); // Display the properties of the authenticated stream. AuthenticatedStreamReporter.DisplayProperties(authStream); // Send a message to the server. // Encode the test data into a byte array. byte[] message = Encoding.UTF8.GetBytes("Hello from the client."); ar = authStream.BeginWrite(message, 0, message.Length, new AsyncCallback(EndWriteCallback), authStream); //</snippet3> ar.AsyncWaitHandle.WaitOne(); Console.WriteLine("Sent {0} bytes.", message.Length); // Close the client connection. authStream.Close(); Console.WriteLine("Client closed."); }
/// <summary> /// Extends BeginWrite so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// negotiatestream.BeginWrite(buffer, asyncCallback); /// </example> /// </summary> public static IAsyncResult BeginWrite(this NegotiateStream negotiatestream, Byte[] buffer, AsyncCallback asyncCallback) { if (negotiatestream == null) { throw new ArgumentNullException("negotiatestream"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } return(negotiatestream.BeginWrite(buffer, 0, buffer.Length, asyncCallback)); }
public async Task NegotiateStream_ConcurrentAsyncReadOrWrite_ThrowsNotSupportedException() { byte[] recvBuf = new byte[s_sampleMsg.Length]; var network = new VirtualNetwork(); using (var clientStream = new VirtualNetworkStream(network, isServer: false)) using (var serverStream = new VirtualNetworkStream(network, isServer: true)) using (var client = new NegotiateStream(clientStream)) using (var server = new NegotiateStream(serverStream)) { await TestConfiguration.WhenAllOrAnyFailedWithTimeout( client.AuthenticateAsClientAsync(CredentialCache.DefaultNetworkCredentials, string.Empty), server.AuthenticateAsServerAsync()); // Custom EndWrite/Read will not reset the variable which monitors concurrent write/read. await TestConfiguration.WhenAllOrAnyFailedWithTimeout( Task.Factory.FromAsync(client.BeginWrite, (ar) => { Assert.NotNull(ar); }, s_sampleMsg, 0, s_sampleMsg.Length, client), Task.Factory.FromAsync(server.BeginRead, (ar) => { Assert.NotNull(ar); }, recvBuf, 0, s_sampleMsg.Length, server)); Assert.Throws <NotSupportedException>(() => client.BeginWrite(s_sampleMsg, 0, s_sampleMsg.Length, (ar) => { Assert.Null(ar); }, null)); Assert.Throws <NotSupportedException>(() => server.BeginRead(recvBuf, 0, s_sampleMsg.Length, (ar) => { Assert.Null(ar); }, null)); } }
private static void Connect(string username = null, string password = null, string host = "localhost", int addressIndex = 1, int port = 11000) { // Get IP address. var ipAddress = Dns.GetHostEntry(host).AddressList[addressIndex]; // Get remote end point. var endPoint = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. _client = new TcpClient(); // Connect the socket to the remote endpoint. _client.Connect(endPoint); Logging.Log($"Client successfully connected to {endPoint}."); // Keep connection alive if transfer isn't complete. _client.LingerState = new LingerOption(true, 0); // Get negotiation stream from client. var negotiateStream = new NegotiateStream(_client.GetStream(), false); // Pass the NegotiateStream as the AsyncState object // so that it is available to the callback delegate. IAsyncResult asyncResult; // If username/password provided, use as credentials. if (username != null && password != null) { asyncResult = negotiateStream.BeginAuthenticateAsClient( new NetworkCredential("username", "password"), host, EndAuthenticateCallback, negotiateStream); } else { // Use Identification ImpersonationLevel asyncResult = negotiateStream.BeginAuthenticateAsClient( EndAuthenticateCallback, negotiateStream ); } Logging.Log("Client attempting to authenticate."); // Await result. asyncResult.AsyncWaitHandle.WaitOne(); // Send encoded test message to server. var message = Encoding.UTF8.GetBytes("Hello, it's me, the client!"); asyncResult = negotiateStream.BeginWrite( message, 0, message.Length, EndWriteCallback, negotiateStream); // Await result. asyncResult.AsyncWaitHandle.WaitOne(); Logging.Log($"Successfully sent message containing {message.Length} bytes."); // Close the client connection. negotiateStream.Close(); Logging.Log("Client closed."); }