Tuesday, November 30, 2021

Posting data to Azure Function using HTTP trigger with .NET 6

Serverless computing was out there for a long time. Microsoft Azure had adapted the concept with Azure Functions, Logic Apps and Event Grids.

We can use Azure Functions for event driven workloads with short lived processes. That means there should be a trigger that will initiate the function. For an example,

  • Blog is added to your container
  • Timer tick
  • New item added to queue
  • HTTP request
  • etc..

This is the first post of a serious of articles on Azure Functions/Durable functions.

In this post I'll guide how to respond to HTTP post request with an Azure Function. 

By the way shouldn't I be using a RESTful Web API in Azure Web App instead of Azure Function?

That can also be an option. But let's think about it's usage. If we have a small and well defined and short lived component, what's the harm of using a function. And I'll extend this article into another blog post to illustrate some cool features Azure Functions can bring to the table.

Let's start the journey

Step 1: Create Azure Function App in Azure portal. I use .Net as the runtime and 6 as the version
















Step 2: Open the VSCode in your directory and install following extensions if you had not already installed















Step 3: Let's navigate to Azure section and login to your subscription. Then click on the Azure Function App we just created. You can see the Function App















Step 4: Let's add our first Function there. Press F1 key and select Azure Functions: Create Function option































Step 5: This is the generated code with the scaffolding


namespace Company.Function
{
    public static class CustomerTrigger
    {
        [FunctionName("CustomerTrigger")]
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

Step 6: Let's add another class called Customer










Step 7: Let's modify the Function a bit now


public static class CustomerTrigger
{
        [FunctionName("CustomerTrigger")]
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            Customer customer = JsonConvert.DeserializeObject(requestBody);


            return new OkObjectResult(customer.name);
        }
}

Step 8: Let's deploy the function to our Function App













Step 9: Great!! our function is deployed now. Let's get the URL











Step 10: Let's try this with Postman














Great!! Our sample is working perfectly !!

Thursday, November 25, 2021

Resolve error: Creation of System Topic has failed with error: The subscription is not registered to use namespace 'Microsoft.EventGrid'. See https://aka.ms/rps-not-found for how to register subscriptions. Event Subscription will not be created.

Recently I got the above error when creating an Event Grid subscription









Following are the steps I used to resolve the error

Using Azure Cloud Shell execute following commands

  • az account set --subscription "Visual Studio Enterprise Subscription"
  • az provider register --namespace Microsoft.EventGrid
  • az provider show -n Microsoft.EventGrid
This will register the provider in your subscription.

This format is applicable for registering any service providers in your subscription

Monday, November 15, 2021

Azure Application Insights - Smart Detection - Identify suspicious user activity

Now we can detect suspicious user activity using Azure Application Insights.

You need to navigate to Application Insights and click on Smart Detection. It will show potential security and performance issues.














We can click on the Suspicious user activity detected (preview) card to obtain more information on the issue. 

Then click on the Suspicious user activity link.








It'll show malicious users who accessed the system from multiple locations at the same time.











If we want we can construct an Alert Rule to notify an authority at a particular time





Furthermore we can see all requests from malicious users. For that you need to click on All requests from the most suspicious user link











You can modify the value for user_AuthenticatedId parameter to see what other users had accessed