使用AWS .NET SDK进行SNS订阅确认示例

Sco*_*son 6 vb.net asp.net-mvc amazon-web-services amazon-sns

我试图弄清楚如何使用AWS .NET SDK来确认订阅SNS主题.

订阅是通过HTTP进行的

端点将位于.net mvc网站中.

我在任何地方找不到任何.net示例?

一个工作的例子太棒了.

我正在尝试这样的事情

 Dim snsclient As New Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceClient(ConfigurationSettings.AppSettings("AWSAccessKey"), ConfigurationSettings.AppSettings("AWSSecretKey"))

    Dim TopicArn As String = "arn:aws:sns:us-east-1:991924819628:post-delivery"


    If Request.Headers("x-amz-sns-message-type") = "SubscriptionConfirmation" Then

        Request.InputStream.Seek(0, 0)
        Dim reader As New System.IO.StreamReader(Request.InputStream)
        Dim inputString As String = reader.ReadToEnd()

        Dim jsSerializer As New System.Web.Script.Serialization.JavaScriptSerializer
        Dim message As Dictionary(Of String, String) = jsSerializer.Deserialize(Of Dictionary(Of String, String))(inputString)

        snsclient.ConfirmSubscription(New Amazon.SimpleNotificationService.Model.ConfirmSubscriptionRequest With {.AuthenticateOnUnsubscribe = False, .Token = message("Token"), .TopicArn = TopicArn})


   End If
Run Code Online (Sandbox Code Playgroud)

cmi*_*lam 9

以下是使用MVC WebApi 2和最新AWS .NET SDK的工作示例.

var jsonData = Request.Content.ReadAsStringAsync().Result;
var snsMessage = Amazon.SimpleNotificationService.Util.Message.ParseMessage(jsonData);

//verify the signaure using AWS method
if(!snsMessage.IsMessageSignatureValid())
    throw new Exception("Invalid signature");

if(snsMessage.Type == Amazon.SimpleNotificationService.Util.Message.MESSAGE_TYPE_SUBSCRIPTION_CONFIRMATION)
{
    var subscribeUrl = snsMessage.SubscribeURL;
    var webClient = new WebClient();
    webClient.DownloadString(subscribeUrl);
    return "Successfully subscribed to: " + subscribeUrl;
}
Run Code Online (Sandbox Code Playgroud)


Sco*_*son -1

我最终使用所示的代码让它工作。我在捕获开发服务器上的异常时遇到了麻烦,结果告诉我服务器的时间与 SNS 消息中的时间戳不匹配。

一旦服务器的时间被修复(顺便说一句,亚马逊服务器),确认就起作用了。