示例#1
0
        public static async void Run([QueueTrigger("orderqueue", Connection = "AzureWebJobsStorage")] POCOOrder order,
                                     TraceWriter log, [SendGrid(ApiKey = "AzureWebJobsSendGridApiKey")] IAsyncCollector <Mail> outputMessage)
        {
            string msg = $"Hello " + order.CustomerName + "! Your order for " + order.ProductName + " with quantity of " + order.OrderCount + " has been shipped.";

            log.Info(msg);

            Mail message = new Mail
            {
                Subject = msg,
                From    = new Email("*****@*****.**", "No Reply"),
                ReplyTo = new Email("*****@*****.**", "Prodip Saha")
            };


            var personalization = new Personalization();

            // Change To email of recipient
            personalization.AddTo(new Email(order.CustomerEmail));

            Content content = new Content
            {
                Type  = "text/plain",
                Value = msg + "Thank you for being such a nice customer!"
            };

            message.AddContent(content);
            message.AddPersonalization(personalization);


            await outputMessage.AddAsync(message);
        }
        public static async Task <HttpResponseMessage> Run(HttpRequestMessage req,
                                                           [HttpTrigger(AuthorizationLevel.User, "post", Route = null)] POCOOrder order, TraceWriter log)
        {
            ClaimsPrincipal claimsPrincipal;
            CustomValidator customValidator = new CustomValidator(log);

            if ((claimsPrincipal = customValidator.ValidateToken(req.Headers.Authorization)) == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.Unauthorized)
                {
                    Content = new StringContent("Invalid Token!", Encoding.UTF8, "application/json")
                });
            }

            string[] allowedRoles = GetEnvironmentVariable("AllowedRoles").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            if (customValidator.IsInRole(claimsPrincipal, allowedRoles) == false)
            {
                return(new HttpResponseMessage(HttpStatusCode.Unauthorized)
                {
                    Content = new StringContent("Invalid Roles!", Encoding.UTF8, "application/json")
                });
            }

            string msg = $"Hello " + order.CustomerName + "! Your order for " + order.ProductName + " with quantity of " + order.OrderCount + " has been received. You will receive email at " + order.CustomerEmail + " as soon as order is shipped.";

            log.Info(msg);

            var connectionString = GetEnvironmentVariable("AzureWebJobsStorage");

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            // Create the queue client.
            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

            // Retrieve a reference to a container.
            CloudQueue queue = queueClient.GetQueueReference("orderqueue");

            // Create the queue if it doesn't already exist
            queue.CreateIfNotExists();

            // Serialize POCO Order
            var orderData = JsonConvert.SerializeObject(order, Formatting.Indented);

            // Create a message and add it to the queue.
            CloudQueueMessage message = new CloudQueueMessage(orderData);

            queue.AddMessage(message);

            // Send an acknowledgement to client app
            return(await Task.Run(() =>
            {
                return new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(msg, Formatting.Indented), Encoding.UTF8, "application/json")
                };
            }));
        }