如何在开源ASP.NET MVC应用程序中保持私有数据分离

时间:2014-04-28 17:25:25

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

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

我如何将这些数据分开并保存在git存储库中,同时仍允许人们在不必设置大量内容的情况下进行拉取和构建?

1 个答案:

答案 0 :(得分: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,它也可以使用外部文件覆盖配置。