请向我转发一些用于开发您自己的 Azure 应用服务站点扩展的链接/操作指南。我正在尝试拦截传入/传出我的应用程序服务的 HTTP 流量,并作为扩展的一部分进行一些预检查/更改一些内容。
我没有找到任何有关为 Microsoft Azure 应用服务开发自定义扩展的文档。
为了在应用程序中添加拦截 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
输出:-
我参考这个MS Document:-
直接登录我的Nuget账户上传包上传后,您的包将被验证 > 发布 > 索引 > 公开可用:-
发布包后,访问您的 Azure 扩展页面,即可看到该包以供安装:-
在高级工具 Kudu > 站点扩展>
参考:-
站点扩展将于 2018 年 8 月转移到 nuget.org · 问题 #87 · Azure/app-service-announcements (github.com)