且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Azure KeyVault注入ASP.NET网站的***方法?

更新时间:2022-06-15 03:03:32

尽管替换令牌方法在Azure devops管道中非常方便且易于使用,正如您所提到的,它给其他开发人员的工作带来了很多麻烦(尤其是对于本地开发).

Though Replace token method is very convenient and easy to use in Azure devops pipeline, as what you mentioned, it leads many trouble to other developer work (especially for the local develop).

为什么不考虑使用

Why not consider to use File transformation task to do this transformate work? This task has one feature variable substitution can let you avoid to any format changes on config file. Just need define corresponding variables which will replace into the config file.

让我举一个示例进行说明,以下是一个简单的web.config文件示例:

Let me take one example to explain this, below is a simple web.config file sample:

<?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:4200http://localhost:8080.

Now I need replace the ClientBasetUrl value: http://localhost:4200 to http://localhost:8080.

1)由于您关注的是将Azure Key Vault与Asp.net Web应用程序结合在一起,因此只需在Azure Key Vault中创建一个秘密ClientBasetUrl,其值为http://localhost:8080.

1) Since what you concerned is combine Azure Key Vault with Asp.net web application, just go and create one secret ClientBasetUrl into Azure key Vault, and its value is http://localhost:8080.

2)将Azure密钥保管库连接到azure devops管道中.

2) Connect Azure key vault into azure devops pipeline.

3):这是关键步骤:请配置File Transform task.

3) Here is the key step: Do configure the 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

然后,您将看到此任务结束后替换成功完成.

Then you will see the replace are finished successfully after this task ended.

您可以看到我不需要对配置文件进行任何语法更改,只需将相应的变量存储在Azure密钥保管库中,并确保可以在管道运行期间下载它们.

You can see I don't need do any syntax change to my config file, just need store corresponding variables in Azure key vault and ensure they can be downloaded during the pipeline running.

而且,它可以使我在本地开发工作中的工作非常顺利.

Also, it can make me go very smoothly on my local develop work.