C# AWS SNS Subscribtion configuration
Install Nuget Dependency
public class Messages
{
public string Type { get; set; }
public string MessageId { get; set; }
public string Token { get; set; }
public string TopicArn { get; set; }
public object Message { get; set; }
//public Message Message2 { get; set; }
public string SubscribeURL { get; set; }
public string Timestamp { get; set; }
public string SignatureVersion { get; set; }
public string Signature { get; set; }
public string SigningCertURL { get; set; }
public string Subject { get; set; }
public string UnsubscribeURL { get; set; }
}
[Route("api/awssns")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly ILogger<ValuesController> _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}
[HttpPost("[action]")]
public async Task<IActionResult> webhook()
{
var re = Request;
var headers = re.Headers;
string body = "";
try
{
string messagetype = Request.Headers["x-amz-sns-message-type"];
//if (headers.Contains("Custom"))
//{
// string token = headers.TryGetValue("Custom").First();
//}
//Logger.Debug(messagetype);
_logger.LogInformation("**** MSDSL BEGIN LOG ****");
_logger.LogInformation(messagetype);
//HttpContext.Request.Body.Seek(0, SeekOrigin.Begin);
using (StreamReader stream = new StreamReader(HttpContext.Request.Body))
{
body = stream.ReadToEnd();
// body = "param=somevalue¶m2=someothervalue"
_logger.LogInformation(body);
Messages msg = JsonConvert.DeserializeObject<Messages>(body);
if (msg != null)
{
if (msg.SignatureVersion.Equals("1"))
{
//temp
{
var aws = Amazon.SimpleNotificationService.Util.Message.ParseMessage(body);
bool r = aws.IsMessageSignatureValid();
if (r)
{
_logger.LogInformation("Signature verification succeeded");
}
else
{
Message message = JsonConvert.DeserializeObject<Message>(msg.Message.ToString());
_logger.LogInformation("Signature verification failed");
await new LogManager().Insert(new Client_Log { Message = JsonConvert.SerializeObject(msg.Message), Response = body, Error = "Signature verification failed", timestamp = DateTime.Now }, message.merchantWallet);
return BadRequest("Signature verification failed");
}
}
if (messagetype.Equals("Notification"))
{
_logger.LogInformation("Subject : " + msg.Subject);
Message message = JsonConvert.DeserializeObject<Message>(msg.Message.ToString());
// do what you want
}
else if (messagetype.Equals("SubscriptionConfirmation"))
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
var response = await client.GetAsync(msg.SubscribeURL);
response.EnsureSuccessStatusCode();
}
else if (messagetype.Equals("UnsubscribeConfirmation"))
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
var response = await client.GetAsync(msg.UnsubscribeURL);
response.EnsureSuccessStatusCode();
}
}
else
{
Message message = JsonConvert.DeserializeObject<Message>(msg.Message.ToString());
_logger.LogInformation("Unexpected signature version. Unable to verify signature.");
}
}
else
{
await new LogManager().Insert(new Client_Log { Response = body, Error = "Body Deserialize fail move to line 141", timestamp = DateTime.Now, Message = "" });
}
}
_logger.LogInformation("**** MSDSL END LOG ****");
}
catch (Exception ex)
{
_logger.LogError( ex,ex.Message);
return StatusCode(500,ex);
}
return Ok();
}
}
great very helpful thank you
ReplyDelete