SslStream不会在.NET Core 2.1+中调用InnerStream的BeginRead或ReadAsync(与.NET Framework和.NET Core 2.0相反)

时间:2019-06-03 17:24:20

标签: c# .net-core

我发现在调用SslStream的BeginRead或ReadAsync时,.NET Core 2.1 / 2.2中的SslStream不会调用InnerStream的BeginRead或ReadAsync。对我来说,这似乎是SslStream实现中的一个错误,但也许现在是预期的行为了?

下面的代码通过端口587连接到SMTP服务器smtp-mail.outlook.com,然后使用STARTTLS命令将连接切换到TLS。 MyStream是NetworkStream的包装,我将其传递给SslStream。

用途:

#Import packages
import pandas as pd
import numpy as np

#Read in data file
df = pd.read_csv(r'C:\Users\james\Desktop\Documents\Downloads\Cybersecurity\cybertime.csv')
df.head

#Create bigrams of themes by days, based on cooccurrences weighted by frequencies.
#Iterate rows until new date is found, then compute weighted cooccurrences.
#Weights are products of theme A frequency (freq) and theme B frequency.


#Output the adjacency list.

MyStream类:

using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Net.Sockets;
using System.Net.Security;
using System.Text;

主要方法:

public class MyStream : NetworkStream
{
    public MyStream(TcpClient tc) : base(tc.Client)
    {
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        int bytesRead = 0;

        try
        {
            bytesRead = base.Read(buffer, offset, count);
        }
        finally
        {
        }
        return bytesRead;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        try
        {
            base.Write(buffer, offset, count);
        }
        finally
        {
        }
    }

    public override void WriteByte(byte value)
    {
        try
        {
            base.WriteByte(value);
        }
        finally
        {
        }
    }

    public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
    {
        try
        {
            return base.BeginRead(buffer, offset, size, callback, state);
        }
        finally
        {
        }
    }

    public override int EndRead(IAsyncResult asyncResult)
    {
        int bytesRead = 0;

        try
        {
            bytesRead = base.EndRead(asyncResult);
        }
        finally
        {
        }
        return bytesRead;
    }

    public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
    {

        try
        {
            return base.BeginWrite(buffer, offset, size, callback, state);
        }
        finally
        {
        }
    }

    public override void EndWrite(IAsyncResult asyncResult)
    {
        try
        {
            base.EndWrite(asyncResult);
        }
        finally
        {
        }
    }

    public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
    {
        return Task.Factory.FromAsync<int>(BeginRead(buffer, offset, count, null, null), EndRead);
    }

    public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
    {
        return Task.Factory.FromAsync(BeginWrite(buffer, offset, count, null, null), EndWrite);
    }
}

在.NET Core 2.1+中,在ssl.AuthenticateAsClientAsync行之后将不会调用MyStream.BeginRead,MyStream.EndRead和MyStream.ReadAsync。

在.NET Core 2.0和.NET Framework中,它们按应有的方式被调用。

编辑: 我现在使用最新的Visual Studio 2019和.NET Core 3.0 Preview 5进行了尝试。问题仍然存在。

0 个答案:

没有答案