public async Task <IActionResult> Post() { string jsonData = null; string status = null; if (Request.Body.CanRead) { try { using (var reader = new StreamReader(Request.Body)) { jsonData = reader.ReadToEnd(); } } catch (IOException ioex) { status = ioex.ToString(); } } var sm = Amazon.SimpleNotificationService.Util.Message.ParseMessage(jsonData); LambdaLogger.Log(sm.ToString()); if (sm.IsMessageSignatureValid()) { if (sm.IsSubscriptionType) { // CONFIRM THE SUBSCRIPTION using (_SnsClient = new AmazonSimpleNotificationServiceClient(_AwsCredentials, RegionEndpoint.USEast2)) { try { _confirmSubscriptionResponse = await _SnsClient.ConfirmSubscriptionAsync(request : new ConfirmSubscriptionRequest { TopicArn = sm.TopicArn, Token = sm.Token }); status = _confirmSubscriptionResponse.SubscriptionArn; LambdaLogger.Log(status); } catch (AmazonSimpleNotificationServiceException ex) { return(Content(HandleSNSError(ex))); } } } if (sm.IsNotificationType) { // PROCESS NOTIFICATIONS status = "SNS Subject: " + sm.Subject + " | SNS Message: " + sm.MessageText; //Process of notification / log notification to CloudWatch Logs LambdaLogger.Log(status); } } if (status == null) { status = "MicroService executed with invalid signature and data: \n" + jsonData; } return(Ok(status)); }
public ActionResult Post() { try { var sharedFile = new SharedCredentialsFile(); CredentialProfile devProfile; if (sharedFile.TryGetProfile("dev", out devProfile)) { var assumeRoleResult = new AssumeRoleAWSCredentials(new SessionAWSCredentials(devProfile.Options.AccessKey, devProfile.Options.SecretKey, devProfile.Options.Token), devProfile.Options.RoleArn, "channelsrole"); String messagetype = Request.Headers["x-amz-sns-message-type"]; //If message doesn't have the message type header, don't process it. if (messagetype == null) { return(StatusCode(400)); } var message = string.Empty; using (var reader = new StreamReader(Request.Body)) { message = reader.ReadToEnd(); var sm = Amazon.SimpleNotificationService.Util.Message.ParseMessage(message); // Check the signature and throw an exception if the signature verification fails. if (isMessageSignatureValid(sm.SigningCertURL, sm.Signature, message).Result) { Debug.WriteLine("Signature verification succeeded"); if (sm.IsSubscriptionType) { var model = new Amazon.SimpleNotificationService.Model.ConfirmSubscriptionRequest(sm.TopicArn, sm.Token); AmazonSimpleNotificationServiceClient c = new AmazonSimpleNotificationServiceClient(assumeRoleResult, RegionEndpoint.USWest2); c.ConfirmSubscriptionAsync(model).Wait(); return(Ok()); // CONFIRM THE SUBSCRIPTION } if (sm.IsNotificationType) // PROCESS NOTIFICATIONS { dynamic json = JObject.Parse(sm.MessageText); //extract value: var s3OrigUrlSnippet = json.input.key.Value as string; } } else { Console.WriteLine(">>Signature verification failed"); return(StatusCode(400)); } } //do stuff return(Ok(new { })); } return(Ok(new { })); } catch (Exception ex) { //LogIt.E(ex); return(StatusCode(500, ex)); } }