private bool VerifySlackRequest(HttpRequest request, IHeaderDictionary headers,
StringValues slackSignatureHeader)
{
string SlackSigningSecret = ConfigurationHelper.SlackSigningSecret;
DateTime timestamp = DateTimeOffset.FromUnixTimeSeconds(timestampUnix).UtcDateTime;
DateTime currentTimestamp = DateTime.UtcNow;
if (Math.Abs((currentTimestamp - timestamp).TotalSeconds) > 60 * 5)
{
// The request timestamp is more than five minutes from local time.
// It could be a replay attack, so let's ignore it.
return false;
}
// Get the request body as a URL-encoded string
string requestBody = string.Join("&", request.Form.Select(kvp => $"{kvp.Key}={kvp.Value}"));
var encoding = new UTF8Encoding();
using (var hmac = new HMACSHA256(encoding.GetBytes(SlackSigningSecret)))
{
var hash = hmac.ComputeHash(encoding.GetBytes($"v0:{headers["X-Slack-Request-Timestamp"]}:{requestBody}"));
var hashString = $"v0={BitConverter.ToString(hash).Replace("-", "").ToLower(CultureInfo.InvariantCulture)}";
if (hashString.Equals(slackSignatureHeader)) return true;
else return false;
}
}
Я хочу проверить команды Slack APIS и интерактивные конечные точки. Я следовал документации, как здесь https://api.slack.com/authentication/verifying-requests-from-slack , и попытался преобразовать ее в код C# и эта функция [code]private bool VerifySlackRequest(HttpRequest request, IHeaderDictionary headers,
if (Math.Abs((currentTimestamp - timestamp).TotalSeconds) > 60 * 5) { // The request timestamp is more than five minutes from local time. // It could be a replay attack, so let's ignore it. return false; }
// Get the request body as a URL-encoded string string requestBody = string.Join("&", request.Form.Select(kvp => $"{kvp.Key}={kvp.Value}"));
var encoding = new UTF8Encoding(); using (var hmac = new HMACSHA256(encoding.GetBytes(SlackSigningSecret))) { var hash = hmac.ComputeHash(encoding.GetBytes($"v0:{headers["X-Slack-Request-Timestamp"]}:{requestBody}")); var hashString = $"v0={BitConverter.ToString(hash).Replace("-", "").ToLower(CultureInfo.InvariantCulture)}"; if (hashString.Equals(slackSignatureHeader)) return true; else return false; } } [/code] и вот как я вызываю эту функцию [code] bool isValidRequest = VerifySlackRequest(Request, Request.Headers, slackSignatureHeader);
if (!isValidRequest) { return Unauthorized("Invalid request signature"); } [/code] [b]Я проверяю SlackSigningSecret, но slackSignatureHeader и hashString всегда не совпадают[/b]