【问题标题】:How to keep private data separate in an open source ASP.NET MVC application如何在开源 ASP.NET MVC 应用程序中保持私有数据分离
【发布时间】:2014-06-14 08:57:25
【问题描述】:

在我正在开发的一个开源 ASP.NET 应用程序中,我需要将配置文件中的某些数据保密,同时仍然让人们可以轻松地在自己的机器上构建和调试它。这是 API 密钥、邮件设置等数据。

我如何将这些数据单独保存在 git 存储库之外,同时仍然允许人们只需拉取和构建而无需设置一堆东西?

【问题讨论】:

    标签: asp.net asp.net-mvc git visual-studio


    【解决方案1】:

    在你的配置文件中你可以定义 configSource:

    <configuration>
       <appSettings configSource="filepath1.config" />   
       <connectionStrings configSource="filepath2.config" />
       <!--etc-->
    </configuration>
    

    将您需要保密的配置放在一个单独的配置文件中,然后将它们排除在您的 .gitignore 中。

    请记住,这将忽略整个部分并用您在引用文件中的上下文覆盖它。

    您也可以使用Configuration Transform,它只允许您在部分中覆盖一小部分变量。例如:

    在您的主 Web.config 中:

    <configuration>
       <appSettings>
            <add key="Key1" value="Something I dont't Care"/>
            <add key="Key2" value="Something dummy"/>
       </appSettings>   
    </configuration>
    

    在您的 Web.Release.config 中:

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
       <appSettings>
          <add key="Key2" value="Something I want to keep secret"
             xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
       </appSettings>   
    </configuration>
    

    在这种情况下,您要保持私有的“Key2”值将位于单独的文件中,您可以通过 .gitignore 排除 Web.Release.config。

    还有我从未尝试过的another approach,它也可以使用外部文件覆盖配置。

    【讨论】:

    • 添加“configSource”属性时,出现错误。在线查看我需要启用“使用外部配置源文件”,但没有关于我可以在哪里启用它的消息。
    • 你能发布错误信息吗?另请查看this 是否适合您。
    • "无法识别的属性 'configSource'。请注意,属性名称区分大小写。"
    • 您尝试将 configSource 添加到哪个部分?您可以发布您尝试过的部分 xml 吗?
    • &lt;smtp configSource="Mail.config" /&gt; 进一步看,我看到有人声称不再支持 configSource。
    猜你喜欢
    • 2013-12-18
    • 2015-03-04
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 2014-05-07
    相关资源
    最近更新 更多