Ler*_*roi 1 asp.net asp.net-mvc content-security-policy identityserver4
我有一个连接到我的身份服务器的微前端。为了让用户访问微前端,用户需要通过该ID服务器进行身份验证。微前端需要嵌入到我的主应用程序的 iframe 中,但由于该微前端需要 ID 服务器,因此我在控制台上收到错误。在嵌入这个微前端之前一切都工作正常,但是当它进入 iframe 时我就遇到了这个问题。
Refused to frame 'https://localhost:44300/' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'none'".
Run Code Online (Sandbox Code Playgroud)
我尝试将其包含在我的身份服务器的 web.config 文件中,这导致控制台上出现上述错误。
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy" value="frame-ancestors ''" />
</customHeaders>
</httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
ID 服务器似乎不允许将其内容嵌入 iframe 中。这可能不是一个好的做法,但根据我的要求,我想知道如何实现这一点。
更新
这是定义 csp 的地方
public override void OnResultExecuting(ResultExecutingContext context)
{
var result = context.Result;
if (result is ViewResult)
{
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
if (!context.HttpContext.Response.Headers.ContainsKey("X-Content-Type-Options"))
{
context.HttpContext.Response.Headers.Add("X-Content-Type-Options", "nosniff");
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
if (!context.HttpContext.Response.Headers.ContainsKey("X-Frame-Options"))
{
context.HttpContext.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
var csp = "default-src 'self'; object-src 'none'; frame-ancestors 'none'; sandbox allow-forms allow-same-origin allow-scripts; base-uri 'self';";
// also consider adding upgrade-insecure-requests once you have HTTPS in place for production
//csp += "upgrade-insecure-requests;";
// also an example if you need client images to be displayed from twitter
// csp += "img-src 'self' https://pbs.twimg.com;";
// once for standards compliant browsers
if (!context.HttpContext.Response.Headers.ContainsKey("Content-Security-Policy"))
{
context.HttpContext.Response.Headers.Add("Content-Security-Policy", csp);
}
// and once again for IE
if (!context.HttpContext.Response.Headers.ContainsKey("X-Content-Security-Policy"))
{
context.HttpContext.Response.Headers.Add("X-Content-Security-Policy", csp);
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
var referrer_policy = "no-referrer";
if (!context.HttpContext.Response.Headers.ContainsKey("Referrer-Policy"))
{
context.HttpContext.Response.Headers.Add("Referrer-Policy", referrer_policy);
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试使用 iframe IdentityServer 确实是一个坏主意,因为这样用户就很难知道他实际登录的位置。
如果您仍想更改它,则需要查看QuickStart文件夹中的SecurityHeadersAttribute.cs文件,该文件定义了 CSP。
在此文件中,您可以调整发送到浏览器的安全标头。您需要查看 CSP 和 X-Frame-Options 标头。
| 归档时间: |
|
| 查看次数: |
6772 次 |
| 最近记录: |