NegotiateStream.Write(Byte[], Int32, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
使用指定的缓冲区和偏移量将指定的 s 数 Byte写入基础流。
public:
override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write(byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)
参数
例外
buffer 是 null。
正在进行写入操作。
此对象已关闭。
身份验证未发生。
示例
下面的代码示例演示如何写入 .NegotiateStream
public static void Main(String[] args)
{
// 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);
DisplayAuthenticationProperties(authStream);
DisplayStreamProperties(authStream);
if (authStream.CanWrite)
{
// Encode the test data into a byte array.
byte[] message = System.Text.Encoding.UTF8.GetBytes("Hello from the client.");
authStream.Write(message, 0, message.Length);
authStream.Flush();
Console.WriteLine("Sent {0} bytes.", message.Length);
}
// Close the client connection.
authStream.Close();
Console.WriteLine("Client closed.");
}
注解
如果启用了加密、签名或加密和签名,则此方法从缓冲区读取数据、加密、签名或加密和签名,并使用基础流传输数据。 如果未使用任何安全服务(如数据加密或签名),此方法将在基础流上调用 Write 。
此方法在写入操作完成时会阻止。 若要防止操作完成时阻止,请使用 WriteAsync 该方法。
在成功进行身份验证之前,无法调用此方法。 若要进行身份验证,请调用其中一种AuthenticateAsClient、AuthenticateAsClientAsync、、BeginAuthenticateAsClientAuthenticateAsServer、AuthenticateAsServerAsync或BeginAuthenticateAsServer方法。
该 NegotiateStream 类不支持多个同时写入操作。 如果尝试在同一流上执行另一个写入操作时启动写入操作, NotSupportedException 则会引发异常。