NegotiateStream.Read Method

Reads data from this stream and stores it in the specified array.

Namespace:  System.Net.Security
Assembly:  System (in System.dll)

public override int Read(
	byte[] buffer,
	int offset,
	int count


Type: System.Byte[]
A Byte array that receives the bytes read from the stream.
Type: System.Int32
A Int32 containing the zero-based location in buffer at which to begin storing the data read from this stream.
Type: System.Int32
A Int32 containing the maximum number of bytes to read from the stream.

Return Value

Type: System.Int32
A Int32 value that specifies the number of bytes read from the underlying stream. When there is no more data to be read, returns 0.


The read operation failed.


Authentication has not occurred.


A Read operation is already in progress.

The method reads a maximum of count bytes from the current stream and stores them in buffer beginning at offset.

You cannot call this method until you have successfully authenticated. To authenticate, call one of the AuthenticateAsClient, BeginAuthenticateAsClient, AuthenticateAsServer, or BeginAuthenticateAsServer methods.

To perform this operation asynchronously, use the BeginRead method.

The following code example demonstrates reading from a NegotiateStream.

public static void AuthenticateClient(TcpClient clientRequest)
    NetworkStream stream = clientRequest.GetStream(); 
    // Create the NegotiateStream.
    NegotiateStream authStream = new NegotiateStream(stream, false);
    // Perform the server side of the authentication.
    // Display properties of the authenticated client.
    IIdentity id = authStream.RemoteIdentity;
    Console.WriteLine("{0} was authenticated using {1}.", 
    // Read a message from the client.
    byte [] buffer = new byte[2048];
    int charLength = authStream.Read(buffer, 0, buffer.Length);
    string messageData = new String(Encoding.UTF8.GetChars(buffer, 0, buffer.Length));

    Console.WriteLine("READ {0}", messageData);
    // Finished with the current client.
    // Close the client connection.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft