我正在Traefik反向代理后面运行.NET Core应用程序的多个实例。当用户访问网站时,Traefik发回StickyCookie(在Set-Cookie内部):
它告诉客户端Traefik后面的哪个服务器接受了他的请求。如果我们想再次向同一服务器发送请求,则还必须在请求中包含cookie。
如何在.NET Core中实现中间件,该中间件会将StickyCookie字段附加到每个请求?它应该包含与先前响应相同的StickyCookie。
基本上,我想实现与以下Linux命令相同的功能:
curl -v --cookie "StickyCookie=http://10.0.2.75:80" http://example.com:5000
答案 0 :(得分:0)
可以注册中间件以在响应头中发送任何内容。您可以在startup.cs文件的Configure方法中注册中间件:
app.UseMiddleware<CustomHeader>();
然后您的CustomHeader类可以是
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PEG.Api.Middleware
{
public class CustomHeader
{
private readonly RequestDelegate _next;
public CustomHeader(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
//you can add your stickyCookie in the dictionary
var dictionary = new Dictionary<string, string[]>()
{
/* add your sticky cookie here. An example below
{
"Access-Control-Allow-Credentials",new string[]{"true" }
}*/
};
//To add Headers AFTER everything you need to do this
context.Response.OnStarting(state => {
var httpContext = (HttpContext)state;
foreach (var item in dictionary)
{
httpContext.Response.Headers.Add(item.Key, item.Value);
}
return Task.FromResult(0);
}, context);
await _next(context);
}
}
}
答案 1 :(得分:0)
您可以在中间件中获得StickyCookie
作为响应头,然后将其保存到Session中,然后可以从会话中检索数据并将其添加到请求头中。
public class AddHeaderMiddleware
{
private readonly RequestDelegate _next;
public AddHeaderMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var data = context.Session.GetString("StickyCookie");
context.Request.Headers.Add("StickyCookie", data);
context.Response.OnStarting(state =>
{
var httpContext = (HttpContext)state;
var cookies = httpContext.Response.Headers["Set-Cookie"].ToString().Split(";");
if (cookies.Length > 0)
{
foreach (var item in cookies)
{
if (item.Split("=")[0] == "StickyCookie")
{
var cookie = item.Split("=")[1];
httpContext.Session.SetString("StickyCookie", cookie);
}
}
}
return Task.FromResult(0);
}, context);
await _next(context);
}
}
public static class AddHeaderMiddlewareExtensions
{
public static IApplicationBuilder UseAddHeader(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<AddHeaderMiddleware>();
}
}
在Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
//// Set a short timeout for easy testing.
//options.IdleTimeout = TimeSpan.FromMinutes(10);
//options.Cookie.HttpOnly = true;
//// Make the session cookie essential
options.Cookie.IsEssential = true;
});
}
public void Configure(IApplicationBuilder app)
{
//...
app.UseSession();
app.UseAddHeader();
//...
}
要检查的操作:
var header = HttpContext.Request.Headers;