如何使用C#验证AWS SNS消息?

时间:2019-05-22 12:35:55

标签: authentication message amazon-sns

我正在将SNS消息发送到HTTP端点。我可以从JSON数据中提取消息信息,但是如何验证消息并验证消息签名?

这是消息之一:

{
  "Type": "Notification",
  "MessageId": "a1825ceb-aa86-531a-9712-09b49bb60b32",
  "TopicArn": "arn:aws:sns:us-west-2:xxxx:Test_Topic",
  "Message": "This is the message body",
  "Timestamp": "2019-05-22T11:13:52.513Z",
  "SignatureVersion": "1",
  "Signature": "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc",
  "SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-abcabcabc.pem",
  "UnsubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:yyy",
  "MessageAttributes": {
    "String2": {
      "Type": "String",
      "Value": "This is the second string"
    },
    "String1": {
      "Type": "String",
      "Value": "This is the first string"
    }
  }
}

如何检查消息签名是否有效?

1 个答案:

答案 0 :(得分:0)

我正在使用AWSSDK Utils中的Message类,这是代码。

var msg = await ReadBody();

var message = Message.ParseMessage(msg);

if (!message.IsMessageSignatureValid())

这是程序包Amazon.SimpleNotificationService.Util.Message。

希望这会有所帮助。