将 Azure KeyVault 注入 ASP.NET 网站的最佳方式?
Best way to inject Azure KeyVault into a ASP.NET Web Site?
我继承了一个旧的项目前风格 ASP.NET 网站(您只打开一个文件夹,没有解决方案文件)。我能够将其转换为稍新的 ASP.NET 网站模板,所以现在我有了解决方案文件。 (在那之后我确实尝试转换为 Web 应用程序,但它变得非常糟糕 - 似乎它需要重新编写,除非我搞砸了,这总是可能的)。
我们正在迁移此应用程序以通过 Azure DevOps Pipelines 进行部署,但我对如何处理机密(特别是连接字符串)有点困惑。我们已经为其他应用程序设置了 Key Vaults 并且一切正常,但是网站模板没有像 Web 应用程序模板那样很好的集成。
现在,我可以将占位符放在 Web 配置中,并使用管道将值替换为 Key Vault,这会工作得很好,但本地开发就不会很容易了(他们必须 copy/paste 我想每次都是本地网络配置)。
有人使用网站成功地做到了这一点,还是我只是想用一种从未被提及的旧技术来做一些事情?
谢谢!
虽然Replace token方法在Azure devops pipeline中非常方便易用,但是如您所说,它会给其他开发人员工作带来很多麻烦(尤其是对于本地开发)。
为什么不考虑使用 File transformation 任务来完成这个转换工作?此任务有一项功能变量替换 可以让您避免对配置文件进行任何格式更改。只需要定义相应的变量替换到配置文件中即可。
让我举一个例子来解释一下,下面是一个简单的web.config
文件示例:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="apiConfig" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<apiConfig>
<add key="ClientBasetUrl" value="http://localhost:4200" />
</apiConfig>
<system.web>
<compilation debug="true" targetFramework="4.6.2">
<assemblies>
<add assembly="System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.6.1" maxRequestLength="20480" />
</system.web>
</configuration>
现在我需要将 ClientBasetUrl
值:http://localhost:4200
替换为 http://localhost:8080
。
1) 由于您关心的是将 Azure Key Vault 与 Asp.net Web 应用程序相结合,只需在 Azure Key Vault 中创建一个秘密 ClientBasetUrl
, 其值为 http://localhost:8080
.
2) 将 Azure 密钥保管库连接到 azure devops 管道。
3) 这是关键步骤:配置 File Transform task
。
steps:
- task: FileTransform@2
displayName: 'File Transform with Variable: '
inputs:
folderPath: '$(System.DefaultWorkingDirectory)'
xmlTargetFiles: MonoApp.config //Here put your config file name that relative to the root folder
然后你会看到这个任务结束后替换成功完成。
你可以看到我不需要对我的配置文件做任何语法更改,只需要在 Azure 密钥保管库中存储相应的变量并确保它们可以在管道期间下载 运行。
另外,它可以让我在本地开发工作上非常顺利。
我继承了一个旧的项目前风格 ASP.NET 网站(您只打开一个文件夹,没有解决方案文件)。我能够将其转换为稍新的 ASP.NET 网站模板,所以现在我有了解决方案文件。 (在那之后我确实尝试转换为 Web 应用程序,但它变得非常糟糕 - 似乎它需要重新编写,除非我搞砸了,这总是可能的)。
我们正在迁移此应用程序以通过 Azure DevOps Pipelines 进行部署,但我对如何处理机密(特别是连接字符串)有点困惑。我们已经为其他应用程序设置了 Key Vaults 并且一切正常,但是网站模板没有像 Web 应用程序模板那样很好的集成。
现在,我可以将占位符放在 Web 配置中,并使用管道将值替换为 Key Vault,这会工作得很好,但本地开发就不会很容易了(他们必须 copy/paste 我想每次都是本地网络配置)。
有人使用网站成功地做到了这一点,还是我只是想用一种从未被提及的旧技术来做一些事情?
谢谢!
虽然Replace token方法在Azure devops pipeline中非常方便易用,但是如您所说,它会给其他开发人员工作带来很多麻烦(尤其是对于本地开发)。
为什么不考虑使用 File transformation 任务来完成这个转换工作?此任务有一项功能变量替换 可以让您避免对配置文件进行任何格式更改。只需要定义相应的变量替换到配置文件中即可。
让我举一个例子来解释一下,下面是一个简单的web.config
文件示例:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="apiConfig" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<apiConfig>
<add key="ClientBasetUrl" value="http://localhost:4200" />
</apiConfig>
<system.web>
<compilation debug="true" targetFramework="4.6.2">
<assemblies>
<add assembly="System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.6.1" maxRequestLength="20480" />
</system.web>
</configuration>
现在我需要将 ClientBasetUrl
值:http://localhost:4200
替换为 http://localhost:8080
。
1) 由于您关心的是将 Azure Key Vault 与 Asp.net Web 应用程序相结合,只需在 Azure Key Vault 中创建一个秘密 ClientBasetUrl
, 其值为 http://localhost:8080
.
2) 将 Azure 密钥保管库连接到 azure devops 管道。
3) 这是关键步骤:配置 File Transform task
。
steps:
- task: FileTransform@2
displayName: 'File Transform with Variable: '
inputs:
folderPath: '$(System.DefaultWorkingDirectory)'
xmlTargetFiles: MonoApp.config //Here put your config file name that relative to the root folder
然后你会看到这个任务结束后替换成功完成。
你可以看到我不需要对我的配置文件做任何语法更改,只需要在 Azure 密钥保管库中存储相应的变量并确保它们可以在管道期间下载 运行。
另外,它可以让我在本地开发工作上非常顺利。