How to create your own App Service Site Extension in Azure?

145 Views Asked by At

Please forward me some links/how-to guides for developing your own App service site extension for Azure. I am trying to intercept HTTP traffic going to/coming out from my App service and do some pre-checks / alter a few things as a part of the extension.

I didn't find any documents on developing your custom extension for Microsoft Azure App Service.

Can this extension be implemented in Golang? If yes, please share examples too.

1

There are 1 best solutions below

0
SiddheshDesai On BEST ANSWER

In order to add the custom extension that intercepts the Http Requests in your application, You need to create one custom Nuget package built with your extension and publish it as a package in nuget.org.

I created one Asp.NET MVC application:-

Added one class that intercepts traffic:-

TrafficInterceptorMiddleware.cs:-

using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

public class TrafficInterceptorMiddleware
{
    private readonly RequestDelegate _next;

    public TrafficInterceptorMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        // Perform pre-checks or modifications here before the request is processed

        if (context.Request.Path.StartsWithSegments("/admin"))
        {
            // Perform pre-checks for requests to /admin endpoint
            // For instance, you can perform authentication, logging, etc.
            // Example: 
            // Check if the user is authenticated
            if (!context.User.Identity.IsAuthenticated)
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                await context.Response.WriteAsync("Unauthorized Access");
                return;
            }
        }

        // Before handling the request further, you can put your logic here.

        await _next(context);

        // Perform actions after the response has been generated (if needed)
        // For example:
        // Log response details, modify response headers, etc.
    }
}

Called this package in my Program.cs:-

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(args);

// Add services
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Add middleware to the pipeline
app.UseMiddleware<TrafficInterceptorMiddleware>();

// Configure routes and endpoints
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Now, I built this project and added the .dll path in the .nuspec file.

Create one .nuspec file with the contents below:-

Traffic.nuspec

In order for your package to be added in Azure Site Extension add this line of code in your .nuspec:-

<tags>AzureSiteExtension</tags>
        <packageTypes>
            <packageType name="AzureSiteExtension" />
        </packageTypes>

Complete code:-

<?xml version="1.0"?>
<package>
    <metadata>
        <id>YourMiddlewarePackageId</id>
        <version>1.0.0</version>
        <title>TrafficInterceptorMiddleware</title>
        <authors>Siddhesh</authors>
        <owners>Siddhesh</owners>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Middleware for intercepting HTTP traffic in ASP.NET 6.0 applications.</description>
        <dependencies>
            <dependency id="Microsoft.AspNetCore.Http" version="6.0.0" />
        </dependencies>
        <releaseNotes>Release notes for version 1.0.0.</releaseNotes>
        <tags>AzureSiteExtension</tags>
        <packageTypes>
            <packageType name="AzureSiteExtension" />
        </packageTypes>
    </metadata>
    <files>
        <file src="bin\Debug\net6.0\WebApplication40.dll" target="lib\net6.0" />
        <file src="README.md" target="" />
    </files>
</package>

Now, Add run the command below in your nuget CLI:-

You can add nuget.exe in your current project and run the command from the Visual Studio Command Line tool too:-

nuget pack Traffic.nuspec

Output:-

enter image description here

I directly uploaded the package by logging into my Nuget account by referring this MS Document:-

enter image description here

After uploading your package will get validated > published > indexed > publicly available:-

enter image description here

After your package is published, Visit your Azure extensions page and the Package is visible for installation:-

enter image description here

In Advanced Tools Kudu > Site Extensions>

enter image description here

Reference:-

Site Extensions are moving to nuget.org by August 2018 · Issue #87 · Azure/app-service-announcements (github.com)