我遇到了一个棘手的问题。我的问题是我使用的是带有jwt的asp.net core 2.0网站。当JWT传递令牌时,它有一个头名称作为身份验证现在我需要添加Nginx的HTTP Basic Auth,它也使用相同的头。登录后,Nginx会显示403,因为它会覆盖标题。
我需要更改JWT标头或Nginx标头中的任何一个。
我在Startup.ConfigureServices中解决了这个问题
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
};
});
在Startup.Configure中
app.UseAuthentication();