asp.net-core - CORB OpenID Connect /connect/authorize 端点(Identity Server 4)
问题描述
我目前遇到了一个奇怪的问题。我们正在托管一个使用 Identity Server 4 作为联合网关的 ASP.CORE 2.1 应用程序。
最初使用 Chrome 导航到该站点时,它是空的 - 在控制台中只留下一个小提示,表明 CORB 已阻止某些内容。
调查 fiddler 似乎它阻止了回发到应用程序/signin-oidc中间件的“隐藏”表单。
但奇怪的是,刷新后一切正常,CORB 不再是问题。查看提琴手中相应的“好”响应,相同的标题是相同的。
我可以验证来自 Identity Server 的 form_post 没有到达提琴手中的应用程序,调试 OpenID 事件和每个请求,我得到了 nada。
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
// look to see if bearer is present instead of cookies
string auth = context.Request.Headers["Authorization"];
if (string.IsNullOrEmpty(auth) || !auth.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
// request needs to be authenticated
await context.ChallengeAsync();
}
else
{
await next();
}
}
else
{
// all is good, continue
await next();
}
});
为什么 CORB 会阻止初始请求?它可以是浏览器导航与刷新中的一种行为吗?
这是我正在调试的事件的客户端配置:
services.AddAuthentication(opts =>
{
opts.DefaultChallengeScheme = "oidce";
opts.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(opts =>
{
opts.SessionStore = new MemoryCacheTicketRepository();
opts.ExpireTimeSpan = TimeSpan.FromHours(2);
})
.AddIdentityServerAuthentication("Bearer", opts =>
{
...
})
.AddOpenIdConnect("oidce", opts =>
{
...
opts.Events = new Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents()
{
OnAuthenticationFailed = async p =>
{
var test = p;
},
OnAuthorizationCodeReceived = async p =>
{
var test = p;
},
OnMessageReceived = async p =>
{
var test = p;
},
OnRemoteFailure = async p =>
{
var test = p;
},
OnTicketReceived = async p =>
{
var test = p;
},
OnTokenResponseReceived = async p =>
{
var test = p;
},
OnTokenValidated = async p =>
{
var test = p;
}
};
});
'Access-Control-Allow-Headers'
由于在好的或坏的响应中出现的标题没有区别,我猜'Content-Type'
这不是问题。会是什么呢?
解决方案
按照 Vidmantas Blazevicius 的评论,我尝试添加 CSPS 标头,它确实有效。
小问题,起初我在为 SPA 服务的 ASP CORE 应用程序中应用了标头。
app.Use(async (context, next) =>
{
context.Response.Headers.Add("Content-Security-Policy",
"default-src 'self' https://localhost:5000; script-src 'unsafe-eval'; script-src-elem 'unsafe-inline';");
if (!context.User.Identity.IsAuthenticated)
{
// look to see if bearer is present instead of cookies
string auth = context.Request.Headers["Authorization"];
if (string.IsNullOrEmpty(auth) || !auth.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
// request needs to be authenticated
await context.ChallengeAsync();
}
else
{
await next();
}
}
else
{
// all is good, continue
await next();
}
});
这似乎并没有真正起到作用,所以我尝试将它添加到应用程序正在服务的 index.html 中。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' https://localhost:5000; script-src 'unsafe-inline' 'unsafe-eval' 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;">
到目前为止,它似乎确实有效。
推荐阅读
- html - 如何摆脱细节元素内元素下方的空间
- javascript - 从自动执行的 .js 脚本自动执行 .vbs 脚本。(没有用户干预)?
- sql - 查找所有拥有工程师和分析师的部门。使用 Join 并且不要忘记 NULL 值
- c# - c# WPF 如何使用字符串变量调用或调用按钮函数,字符串变量包含按钮名称?
- javascript - 锁定正文滚动,但正文中的某些 div 可以滚动
- java - 两个几乎相同的方面导致奇怪的问题
- c# - 无法使用 C# 和管理员用户设置密码和启用帐户
- django - Django - 从模型中获取特定数据
- python - Python urllib3:一段时间后关闭空闲连接
- python - 如何针对 list_b 中的每个元素检查 list_a 的一个索引,并对 list_a 中的所有元素重复此过程?