从客户端到服务器的请求中剥离的标头

时间:2019-05-21 09:34:18

标签: apache proxy .net-core angular7 kestrel

最近,我已将包含angular 7客户端和asp .net核心web api(.net核心v2.2)的简单应用程序转移到生产环境中,遇到了一个奇怪的问题。当我尝试执行一个操作,该操作应该在标头中使用身份验证令牌发出发布请求时,出现了401-未经授权的服务器错误。

我的生产环境是Ubuntu 18.04,Apache2服务器的代理设置为从端口80重定向到端口5000(这是API所在的地方)。

在开发环境中,一切正常运行,没有任何错误,因此我想有些事情可以将请求标头从apache传输到kestrel。

我试图用Google搜索这个问题,并且在Microsoft网站上发现我需要在startup.cs文件中使用转发标头方法,如下所示:

app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseAuthentication();

我像这样配置了启动类,但没有成功。 任何寻求解决方案的帮助或指导将不胜感激。 预先非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试通过中间件添加自定义标头。

namespace Middleware
{
public class CustomHeader
{
    private readonly RequestDelegate _next;

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

    public async Task Invoke(HttpContext context,
    {

        var dictionary = new Dictionary<string, string[]>()
        {
            {
                "Access-Control-Allow-Headers",new string[]{ "authorization" }
            }
        };

        //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);
    }
}
}