如何在 Azure 中创建自己的应用服务站点扩展?

问题描述 投票:0回答:1

请向我转发一些用于开发您自己的 Azure 应用服务站点扩展的链接/操作指南。我正在尝试拦截传入/传出我的应用程序服务的 HTTP 流量,并作为扩展的一部分进行一些预检查/更改一些内容。

我没有找到任何有关为 Microsoft Azure 应用服务开发自定义扩展的文档。

azure azure-web-app-service azure-appservice azure-deployment
1个回答
0
投票

为了在应用程序中添加拦截 Http 请求的自定义扩展,您需要创建一个使用您的扩展构建的自定义 Nuget 包,并将其作为包发布在

nuget.org.

我创建了一个

Asp.NET
MVC 应用程序:-

添加了一个拦截流量的类:-

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.
    }
}

在我的

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();

现在,我构建了这个项目,并在 .nuspec 文件中添加了 .dll 路径

创建一个

.nuspec file
,内容如下:-

Traffic.nuspec

为了将您的包添加到 Azure 站点扩展中,请在您的

.nuspec
:-

中添加这行代码
<tags>AzureSiteExtension</tags>
        <packageTypes>
            <packageType name="AzureSiteExtension" />
        </packageTypes>

完整代码:-

<?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>

现在,在 nuget CLI 中添加运行以下命令:-

您可以在当前项目中添加

nuget.exe
并从 Visual Studio 命令行工具运行命令:-

nuget pack Traffic.nuspec

输出:-

enter image description here

我参考这个MS Document:-

直接登录我的Nuget账户上传包

enter image description here

上传后,您的包将被验证 > 发布 > 索引 > 公开可用:-

enter image description here

发布包后,访问您的 Azure 扩展页面,即可看到该包以供安装:-

enter image description here

在高级工具 Kudu > 站点扩展>

enter image description here

参考:-

站点扩展将于 2018 年 8 月转移到 nuget.org · 问题 #87 · Azure/app-service-announcements (github.com)

© www.soinside.com 2019 - 2024. All rights reserved.