【问题标题】:SSL/TSL issue when attempting to post message from C# to Slack尝试将消息从 C# 发布到 Slack 时出现 SSL/TLS 问题
【发布时间】:2020-11-11 15:09:47
【问题描述】:

我正在尝试通过 ASP.NET MVC C# 中的 Slack 使用 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 地址/令牌)

   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");
        
    }

我得到的错误如下:

请求被中止:无法创建 SSL/TLS 安全通道。

所以我的 PostMessage 方法似乎有问题,URI 返回。根据我的研究,它应该可以工作!我的网络挂钩在 Slack 中经过验证并正确设置。

非常感谢任何帮助!

【问题讨论】:

  • 只是一个猜测,因为我没有使用过他们的 API - 使用SecurityProtocolType.Tls12,因为他们有deprecated older TLS。 Hth.
  • 请发表您的评论作为解决方案!它解决了我的问题,我想给你信用! TLS12 就是答案。
  • 酷 - 在下面完成。很高兴它修复了它:)

标签: c# asp.net-mvc ssl slack slack-api


【解决方案1】:

松弛requires TLS 1.2 and above

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

参考号:SecurityProtocolType Enum

Hth.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2016-08-22
    • 2020-08-23
    • 2020-12-05
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多