我遇到了一个难题。我的问题是我在jwt中使用asp.net core 2.0网站。当JWT传递令牌时,它具有标头名称作为Authentication,现在我需要添加Nginx的HTTP Basic Auth,它也使用相同的标头。登录后Nginx显示403,因为它覆盖了标题。
我需要尽可能更改JWT标头或Nginx标头。
答案 0 :(得分:1)
答案 1 :(得分:0)
我在Startup.ConfigureServices中解决了这个问题
[[1 2 3]
[4 5 6]
[7 8 9]]
然后在Startup.Configure
中var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.Events = new JwtBearerEvents()
{
OnMessageReceived = context =>
{
var header = context.HttpContext.Request.Headers["custom-header"].ToString();
if (!string.IsNullOrEmpty(header) && header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
context.Token = header.Substring("Bearer ".Length).Trim();
}
return Task.CompletedTask;
}
};
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});