简体   繁体   English

如何在 ASP.Net Core Mvc 应用程序中验证 Slack Events API 请求 URL?

[英]How to verify Slack Events API Request Url in ASP.Net Core Mvc application?

I'm working on Slack integration in an ASP.Net Core MVC 2 application.我正在 ASP.Net Core MVC 2 应用程序中进行 Slack 集成。

I have done it for Searching and Posting messages to Slack channels.我已经完成了搜索和发布消息到 Slack 频道的工作。 Now I'm stuck at integrating Events API in this application.现在我坚持在这个应用程序中集成事件 API。 Basically, at the moment I'm not able to verify my Request Url as mentioned here Events API Subscription基本上,目前我无法验证这里提到的请求 URL事件 API 订阅

Following is my action method that we have give to Slack where they will send the verification json object which will be mapped to request parameter of my action and is as follows:以下是我们提供给 Slack 的操作方法,他们将发送验证 json 对象,该对象将映射到我的操作的请求参数,如下所示:

{
"token": "Jhj5dZrVaK7ZwHHjRyZWjbDl",
"challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P",
"type": "url_verification"
}

[HttpPost]
    public IActionResult Event(EventsRequest request)
    {
        if (request != null)
        {
            if (request.type.Equals("url_verification"))
                return Content(request.challenge);
            else
                ViewBag.Challenge = request.challenge;
        }
        return View();
    }

Here is my EventsRequest class:这是我的 EventsRequest 类:

public class EventsRequest
{
    public string token { get; set; }
    public string challenge { get; set; }
    public string type { get; set; }
}

I have deployed this application locally on IIS and have applied InBound rules to make it accessible publicly and it is accessible.我已在 IIS 上本地部署此应用程序,并应用 InBound 规则使其可公开访问且可访问。 But issues arise when I give the following URL to Slack for verification:但是当我将以下 URL 提供给 Slack 进行验证时会出现问题:

http://IP_Address/Slack/Event http://IP_Address/Slack/Event

Following is the screenshot of the response that Slack gives以下是 Slack 给出的响应截图在此处输入图片说明

Can someone tell what's wrong here?有人可以告诉这里有什么问题吗? I tried to hit this URL with Postman and I was able to get the desired results.我尝试使用 Postman 访问此 URL,并且能够获得所需的结果。

You can try these:你可以试试这些:

  1. As suggested in https://api.slack.com/events-api#subscriptions正如https://api.slack.com/events-api#subscriptions 中所建议的

Your Event Request URL must be confirmed before saving this form.在保存此表单之前,必须确认您的活动请求 URL。 If your server takes some time to "wake up" and your initial attempt at URL verification fails due to a timeout, use the retry button to attempt verification again.如果您的服务器需要一些时间来“唤醒”并且您的 URL 验证初始尝试由于超时而失败,请使用重试按钮再次尝试验证。

If it fails at first attempt make sure server is up & running and try next time.如果第一次尝试失败,请确保服务器已启动并正在运行,并在下次尝试。

  1. After you've completed typing your URL, we'll dispatch a HTTP POST to your request URL.在您完成输入 URL 后,我们将向您的请求 URL 发送 HTTP POST。 We'll verify your SSL certificate and we'll send a application/json POST body containing three fields:我们将验证您的 SSL 证书,并发送一个包含三个字段的 application/json POST 正文:

    { "token": "Jhj5dZrVaK7ZwHHjRyZWjbDl", "challenge": "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P", "type": "url_verification" }

You need to replace example json with the real values generated for you url configuration and verification.您需要使用为您的 url 配置和验证生成的真实值替换示例 json。

Make sure url for verification is set correctly before generating challenge and is not changed afterwards.确保在生成挑战之前正确设置验证 url,并且之后不会更改。

  1. Make sure no mismatch in url scheme like http in one place and https at other.确保 url 方案中没有不匹配,例如一个地方的 http 和另一个地方的 https。

Try adding from body in post request:尝试在 post 请求中从 body 添加:

[HttpPost]
public IActionResult Event([FromBody]EventsRequest request)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM