NegotiateStream Constructors

Definition

Initializes a new instance of the NegotiateStream class.

Overloads

NegotiateStream(Stream)

Initializes a new instance of the NegotiateStream class using the specified Stream.

NegotiateStream(Stream, Boolean)

Initializes a new instance of the NegotiateStream class using the specified Stream and stream closure behavior.

Remarks

To prevent the NegotiateStream from closing the stream that you supply, use the NegotiateStream(Stream, Boolean) constructor.

NegotiateStream(Stream)

Source:
NegotiateStream.cs
Source:
NegotiateStream.cs
Source:
NegotiateStream.cs

Initializes a new instance of the NegotiateStream class using the specified Stream.

C#
public NegotiateStream(System.IO.Stream innerStream);

Parameters

innerStream
Stream

A Stream object used by the NegotiateStream for sending and receiving data.

Examples

The following code example demonstrates calling this constructor.

C#
 // Establish the remote endpoint for the socket.
 // For this example, use the local machine.
 IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
 IPAddress ipAddress = ipHostInfo.AddressList[0];
 // Client and server use port 11000.
 IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
 // Create a TCP/IP socket.
TcpClient 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));
 // Request authentication.
 NetworkStream clientStream = client.GetStream();
 NegotiateStream authStream = new NegotiateStream(clientStream);
 // Request authentication for the client only (no mutual authentication).
 // Authenicate using the client's default credetials.
 // Permit the server to impersonate the client to access resources on the server only.
 // Request that data be transmitted using encryption and data signing.
 authStream.AuthenticateAsClient(
      (NetworkCredential) CredentialCache.DefaultCredentials,
      "",
      ProtectionLevel.EncryptAndSign,
      TokenImpersonationLevel.Impersonation);

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

NegotiateStream(Stream, Boolean)

Source:
NegotiateStream.cs
Source:
NegotiateStream.cs
Source:
NegotiateStream.cs

Initializes a new instance of the NegotiateStream class using the specified Stream and stream closure behavior.

C#
public NegotiateStream(System.IO.Stream innerStream, bool leaveInnerStreamOpen);

Parameters

innerStream
Stream

A Stream object used by the NegotiateStream for sending and receiving data.

leaveInnerStreamOpen
Boolean

true to indicate that closing this NegotiateStream has no effect on innerStream; false to indicate that closing this NegotiateStream also closes innerStream.

Exceptions

innerStream is null.

-or-

innerStream is equal to Null.

Examples

The following example demonstrates calling this constructor. This code example is part of a larger example provided for the NegotiateStream class.

C#
// 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);
// Request authentication.
NetworkStream clientStream = client.GetStream();
NegotiateStream authStream = new NegotiateStream(clientStream, false);

Remarks

When you specify true for the leaveStreamOpen parameter, closing the NegotiateStream has no effect on the innerStream stream; you must explicitly close innerStream when you no longer need it.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1