【发布时间】:2019-12-20 17:04:55
【问题描述】:
我们有一个 HttpSys 监听器,它应该接受 NTLM、Negotiate 或 JWT 身份验证。
问题在于 HttpSys 似乎拒绝了预检消息和带有承载令牌 (JWT) 的消息
我们的监听器是这样构建的
_host = new WebHostBuilder()
.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
options.Authentication.AllowAnonymous = false;
})
.UseUrls($"http://+:{PortNo}/")
.UseUnityServiceProvider(IocContainer)
.ConfigureServices(services => { services.AddSingleton(_startUpConfig); })
.UseStartup<StartUp>()
.Build();
我们将 CORS 和身份验证添加到服务中:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials().WithOrigins("*");
}));
services.AddAuthentication(o =>
{
o.DefaultAuthenticateScheme = HttpSysDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.Events = new JwtBearerEvents { OnTokenValidated = context => AuthMiddleWare.VerifyJwt(context, _jwtPublicKey) };
});
我们在 Chrome 中运行一个 Angular 应用程序,该应用程序被拒绝并显示以下错误消息 “对预检请求的响应未通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。”
任何不记名令牌消息也被拒绝。调试显示我们验证 JWT 承载的代码从未到达 (AuthMiddleWare.VerifyJwt)
我的猜测是 HttpSys 拒绝任何不携带 Ntlm 或 Negotiate 令牌的消息。只有我不知道如何解决这个问题
在 .net 框架中,我们使用 AuthenticationSchemeSelectorDelegate 运行以下代码,它允许 OPTIONS 消息和带有 Bearer 令牌的消息通过 HttpSys 监听器
public AuthenticationSchemes EvaluateAuthentication(HttpListenerRequest request)
{
if (request.HttpMethod == "OPTIONS")
{
return AuthenticationSchemes.Anonymous;
}
if (request.Headers["Authorization"] != null && request.Headers["Authorization"].Contains("Bearer "))
{
return AuthenticationSchemes.Anonymous;
}
return AuthenticationSchemes.IntegratedWindowsAuthentication;
}
【问题讨论】:
-
你打电话给
app.UseCors()吗?你用EnableCors属性装饰你的控制器/动作吗? -
是的,我们使用 app.UseCors("AllowAll");我们没有使用 EnableCors 属性。刚试了一下。它没有任何区别。调试显示我们从未进入管道。请求永远不会来自 HttpSys。所以控制器属性不能解决这个问题。谢谢
标签: c# authentication asp.net-core jwt ntlm