SSL/TSL 尝试从 C# 向 Slack 发送 post 消息时出现问题

SSL/TSL issue when attempting to post message from C# to Slack

我正在尝试 post 通过 Slack 在 ASP.NET MVC C# 中使用 Web 挂钩发送消息。我在尝试执行时遇到 SSL/TLS 问题。我的代码看起来很棒,我已经将它与现有的几个教程进行了比较,没有发现任何差异。这是我的 SlackClient.cs :

public class SlackClient
{
    private readonly Uri _uri;
    private readonly Encoding _encoding = new UTF8Encoding();

    public SlackClient(string urlWithAccessToken)
    {
        _uri = new Uri(urlWithAccessToken);
    }

    //Post a message using simple strings  
    public void PostMessage(string text, string username = null, string channel = null)
    {
        Payload payload = new Payload()
        {
            Channel = channel,
            Username = username,
            Text = text
        };

        PostMessage(payload);
    }

    
    public HttpResponseMessage PostMessage(Payload payload)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
        string payloadJson = JsonConvert.SerializeObject(payload);
        var content = new StringContent(payloadJson, Encoding.UTF8, "application/json");
        using (HttpClient client = new HttpClient())
        { var result = client.PostAsync(_uri, content).Result; return result; }
    }
}

//This class serializes into the Json payload required by Slack Incoming WebHooks  
   public class Payload
   {
    [JsonProperty("channel")]
    public string Channel { get; set; }

    [JsonProperty("username")]
    public string Username { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }
    }
 

这里是我实际调用 PostMessage 的地方(出于安全目的,我已通过实际的 webhook address/token 隐藏)

   public void SlackMessageTest()
    {

        string WebHookUrl = "https://myslackwebsite.slack.com/services/MYWEBHOOKURLFROMSLACK";             
        SlackClient client = new SlackClient(WebHookUrl);
        client.PostMessage(username: "tester", text: "Testing Slack Integration!", channel: "#random");
        
    }

我得到的错误如下:

The request was aborted: Could not create SSL/TLS secure channel.

看来我的 PostMessage 方法和 URI return 有问题。根据我的研究,它应该可以工作!我的网络挂钩已在 Slack 中得到验证和正确设置。

非常感谢任何帮助!!

松弛requires TLS 1.2 and above

也就是说,将 SecurityProtocolType.Tls (TLS 1) 替换为 SecurityProtocolType.Tls12(TLS 1.2)

REF:SecurityProtocolType Enum

Hth.