C# 在请求头中包含来自响应头的cookie

C# 在请求头中包含来自响应头的cookie,c#,asp.net-core,cookies,asp.net-core-2.1,traefik,C#,Asp.net Core,Cookies,Asp.net Core 2.1,Traefik,我正在Traefik反向代理背后运行多个.NET核心应用程序实例。当用户访问网站时,Traefik发回StickyCookie(设置内Cookie): 它告诉客户端Traefik背后的哪个服务器接受了他的请求。如果我们想再次向同一台服务器发送请求,我们还必须在请求中包含cookie 如何在.NET Core中实现中间件,将StickyCookie字段附加到每个请求?它应该包含与从以前的响应中收到的相同的StickyCookie 基本上,我希望实现与以下Linux命令相同的功能: curl-v-

我正在Traefik反向代理背后运行多个.NET核心应用程序实例。当用户访问网站时,Traefik发回StickyCookie(设置内Cookie):

它告诉客户端Traefik背后的哪个服务器接受了他的请求。如果我们想再次向同一台服务器发送请求,我们还必须在请求中包含cookie

如何在.NET Core中实现中间件,将StickyCookie字段附加到每个请求?它应该包含与从以前的响应中收到的相同的StickyCookie

基本上,我希望实现与以下Linux命令相同的功能:


curl-v--cookie“粘滞cookie”=http://10.0.2.75:80" http://example.com:5000

可以注册中间件来发送响应头中的任何内容。您可以在startup.cs文件配置方法中注册中间件:

  app.UseMiddleware<CustomHeader>();
app.UseMiddleware();
然后您的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);
        }
    }
}
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Options;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间PEG.Api.Middleware
{
公共类CustomHeader
{
private readonly RequestDelegate\u next;
公共CustomHeader(RequestDelegate下一步)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext上下文)
{
//您可以在字典中添加粘滞cookie
var dictionary=newdictionary()
{
/*在这里添加你的粘饼干。下面是一个例子
{
“访问控制允许凭据”,新字符串[]{“true”}
}*/
};
//在您需要执行的所有操作之后添加标题
context.Response.onStart(状态=>{
var httpContext=(httpContext)状态;
foreach(字典中的变量项)
{
httpContext.Response.Headers.Add(item.Key,item.Value);
}
返回Task.FromResult(0);
},上下文);
等待下一步(上下文);
}
}
}

您可以在中间件中获取响应头中的
StickyCookie
,然后将其保存到,然后您可以从会话中检索数据并将其添加到请求头中

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>();
    }
}
要检查的操作:

var header = HttpContext.Request.Headers;

我明白这一点。但我不知道如何将
StickyCookie
(我们以前从response收到的)附加到每个客户的请求中?所以工作流是这样的:1。客户打开网站,2。StickyCookie在响应头3中发送。从那时起,每个请求标头中都必须存在相同的StickyCookie。服务器上是否有web API驱动的.net core应用程序和独立的客户端应用程序?否,我在客户端使用.net core MVC。@标记是否要将带有cookie标头的asp.net core(在控制器或中间件中)请求发送到后端服务器(可能是php服务器)?
var header = HttpContext.Request.Headers;