Asp.net 如何为identity server启用内容安全策略?
我有一个微型前端连接到我的身份服务器。为了让用户访问micro前端,需要通过该ID服务器对用户进行身份验证。微前端需要嵌入到我的主应用程序的iframe中,但是由于这个微前端需要ID服务器,我在控制台上遇到了一个错误。在嵌入这个微型前端之前,一切都很好,但我在它进入iframe后就遇到了这个问题Asp.net 如何为identity server启用内容安全策略?,asp.net,asp.net-mvc,identityserver4,content-security-policy,Asp.net,Asp.net Mvc,Identityserver4,Content Security Policy,我有一个微型前端连接到我的身份服务器。为了让用户访问micro前端,需要通过该ID服务器对用户进行身份验证。微前端需要嵌入到我的主应用程序的iframe中,但是由于这个微前端需要ID服务器,我在控制台上遇到了一个错误。在嵌入这个微型前端之前,一切都很好,但我在它进入iframe后就遇到了这个问题 Refused to frame 'https://localhost:44300/' because an ancestor violates the following Content Securi
Refused to frame 'https://localhost:44300/' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'none'".
我试图将其包含在identity server的web.config文件中,这导致了控制台上的上述错误
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy" value="frame-ancestors ''" />
</customHeaders>
</httpProtocol>
</system.webServer>
尝试使用iframe IdentityServer确实不是一个好主意,因为这样用户就很难知道他实际登录的位置 如果仍要更改它,则需要查看QuickStart文件夹中的SecurityHeadersAttribute.cs文件,该文件定义了CSP
在此文件中,可以调整发送到浏览器的安全标题。您需要查看CSP和X-Frame-Options标题。尝试iframe IdentityServer确实是个坏主意,因为这样用户就很难知道他实际登录的位置 如果仍要更改它,则需要查看QuickStart文件夹中的SecurityHeadersAttribute.cs文件,该文件定义了CSP
在此文件中,可以调整发送到浏览器的安全标题。您需要查看CSP和X-Frame-Options标题。谢谢您的回答。为了能够在iframe中加载页面,我可以在这个文件中更改什么?我正在开发一个模块化UI,底层框架使用iframe嵌入所有其他应用程序。这只是一个原型,我正在工作,所以我会改变,稍后可能到另一个框架。我用csp文件内容更新了我的问题。更新了我的答案,希望你能接受这个答案。我删除了框架祖先指令,现在它可以工作了。谢谢你的回答谢谢你的回答。为了能够在iframe中加载页面,我可以在这个文件中更改什么?我正在开发一个模块化UI,底层框架使用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);
}
}
}