【问题标题】:Angular5 deploy to Azure webapp: Content Security PolicyAngular5 部署到 Azure webapp:内容安全策略
【发布时间】:2026-02-13 06:10:01
【问题描述】:

我想将使用 angular-cli 创建的 angular5 应用程序部署到 azure webapp 服务。我做了什么:

新的测试应用程序

ng build --prod --aot

cd 范围

//测试用例的脏部分:

git 初始化

git 添加 *

git commit -m '初始提交'

git 远程添加天蓝色https://user@xxx.scm.azurewebsites.net:443/xxx.git

git push azure master

输出:

成功完成。远程:运行后部署命令... 远程:部署成功。远程:应用程序容器将开始 10 秒内重启。

所以一切看起来都很好。如果我在部署完成后前往 URL,我很遗憾收到:

无法获取/

出现错误:

内容安全策略:页面的设置阻止了自身资源的加载(“default-src http://xxx.azurewebsites.net”)。来源: ;!function(){var t=0,e=function(t,e){ret.... xxx.azurewebsites.net:1

到目前为止,我看到的每一个教程或示例都从未与 CSP 或这方面的任何东西有关。有什么帮助吗?

【问题讨论】:

    标签: angular azure azure-web-app-service angular-cli


    【解决方案1】:

    尝试如下检查 CSP 标头,

    <system.webServer>  
      <httpProtocol>
        <customHeaders>
          <add name="Content-Security-Policy" value="
            default-src 'self';
            style-src 'self' 'unsafe-inline';
            script-src 'self' https://*.vo.msecnd.net;
            connect-src wss: https:;" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>  
    

    您可以参考此文档,该文档将为您提供有关 CSP 的一些信息:https://www.henrihietala.fi/how-to-secure-your-asp-net-azure-web-app/

    另外,您可以参考这个 MSDN 线程:https://social.msdn.microsoft.com/Forums/vstudio/en-US/18425bf2-49d2-4ad6-824d-b84205ab0eea/adding-content-security-policy-xxss-protection-to-my-web-app?forum=windowsazurewebsitespreview,可能会有所帮助。

    【讨论】:

    • 感谢您的回答。我在 dist/ 中创建了一个 web.config 并将其添加到 repo 并推送它。什么都没发生,还是同样的问题。文件的内容是这样的: