从Visual Studio发布并使用aspnet_regiis自动加密appSettings

时间:2022-08-01 18:27:53

I have web application that is deployed to my local IIS server under Sites\Default, it works fine and right now I want to make it more secure - I want to encrypt connection strings and appSettings.
Inside pubxml file I've added this line:

我有部署到Sites \ Default下的本地IIS服务器的Web应用程序,它工作正常,现在我想让它更安全 - 我想加密连接字符串和appSettings。在pubxml文件中我添加了这一行:

<MSDeployEnableWebConfigEncryptRule>true</MSDeployEnableWebConfigEncryptRule>

but that only encrypts connections strings. I know that I can manually call:

但这只加密连接字符串。我知道我可以手动打电话:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pe "appSettings" -site Default -app "/"

on my server after deploy to encrypt external file containing appSettings, but I must do that manually.

在我的服务器上部署后加密包含appSettings的外部文件,但我必须手动执行此操作。

My question is how can I deploy website from Visual Studio (Build > Publish) and have that aspnet_regiis command execute automatically after publish succeeded.

我的问题是如何从Visual Studio(Build> Publish)部署网站,并在发布成功后自动执行aspnet_regiis命令。

I found information that I could use runcommand and other about bat files, but I'm not calling MSDeploy from command line.
I've also found information that I should build custom provider and call it from MSDeploy.

我发现可以使用runco​​mmand和其他有关bat文件的信息,但我不是从命令行调用MSDeploy。我还发现了我应该构建自定义提供程序并从MSDeploy调用它的信息。

How should I edit my pubxml file to get this behaviour?

我应该如何编辑我的pubxml文件以获得此行为?

EDIT1:
I've managed to hook After Deploy Target using:

EDIT1:我设法使用以下方法挂钩After Deploy Target:

<Target Name="EncryptAppSettings" AfterTargets="MSDeployPublish" >
  <Message Text="Encrypting appSettings" />
  <Exec Command="aspnet_regiis -pe &quot;appSettings&quot; -site Default -app &quot;/&quot;" />
  <Message Text="AppPath: $(DeployIisAppPath)" />
</Target>

But now I get this error:

但是现在我收到了这个错误:

The command "aspnet_regiis -pe "appSettings" -site Default -app "/"" exited with code 9009.

命令“aspnet_regiis -pe”appSettings“-site Default -app”/“”退出代码9009。

EDIT2:
I've tried using runCommand like this:

EDIT2:我尝试过像这样使用runCommand:

<ItemGroup>
  <MsDeploySourceManifest Include="runCommand">
    <path>aspnet_regiis -pe &quot;appSettings&quot; -site Default -app &quot;/&quot;</path>
    <waitInterval>10000</waitInterval>
    <AdditionalProviderSettings>waitInterval</AdditionalProviderSettings>
  </MsDeploySourceManifest>
</ItemGroup>

but I had no luck. I found blog about postSync:runCommand, but I'd like to call that directly from VS so I'd like to add that to publish profile.

但我没有运气。我找到了关于postSync:runCommand的博客,但我想直接从VS调用它,所以我想添加它来发布配置文件。

EDIT3:
I'm adding my publish profile below:

编辑3:我在下面添加我的发布配置文件:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <MSDeployServiceURL>192.168.5.50</MSDeployServiceURL>
    <DeployIisAppPath>Default</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
    <EnableMSDeployBackup>True</EnableMSDeployBackup>
    <MSDeployEnableWebConfigEncryptRule>True</MSDeployEnableWebConfigEncryptRule>
    <UserName>LocalAdmin</UserName>
    <_SavePWD>True</_SavePWD>
    <PublishDatabaseSettings>
      <Objects xmlns="">
        <ObjectGroup Name="ApplicationDbContext" Order="1" Enabled="False">
          <Destination Path="Data Source=192.168.5.51;Initial Catalog=GameBit;User ID=GUser;Password=MyRealPassword;Application Name=EntityFramework" Name="Data Source=192.168.5.51;Initial Catalog=GameBit;User ID=GUser;Password=MyRealPassword;MultipleActiveResultSets=True;Application Name=EntityFramework" />
          <Object Type="DbCodeFirst">
            <Source Path="DBContext" DbContext="Api.ApplicationDbContext, Api" Origin="Configuration" />
          </Object>
        </ObjectGroup>
      </Objects>
    </PublishDatabaseSettings>
  </PropertyGroup>

  <PropertyGroup>
    <UseMsdeployExe>true</UseMsdeployExe>
    <AllowUntrustedCertificate>True</AllowUntrustedCertificate>
  </PropertyGroup>

  <ItemGroup>
    <MSDeployParameterValue Include="$(DeployParameterPrefix)ApplicationDbContext-Web.config Connection String">
      <ParameterValue>metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=192.168.5.51;Initial Catalog=GameBit;User ID=GUser;Password=MyRealPassword;MultipleActiveResultSets=True;Application Name=EntityFramework"</ParameterValue>
    </MSDeployParameterValue>
  </ItemGroup>

  <!--<ItemGroup>
  <MsDeploySourceManifest Include="runCommand">
    <Path>dir</Path>
  </MsDeploySourceManifest>
</ItemGroup>-->


  <!--<Target Name="EncryptImportantSettings" AfterTargets="MSDeployPublish" >
    <Message Text="Encrypting appSettings" />
    --><!--<Exec Command="aspnet_regiis -pe &quot;appSettings&quot; -site Default -app &quot;/&quot;" />--><!--
  <ItemGroup>
    <MsDeploySourceManifest Include="runCommand">
      <path>dir/b >> C:\temp\log.txt</path>
      --><!--<waitInterval>10000</waitInterval>--><!--
      --><!--<AdditionalProviderSettings>waitInterval</AdditionalProviderSettings>--><!--
    </MsDeploySourceManifest>
  </ItemGroup>
  <Message Text="AppPath: $(DeployIisAppPath)" />
  </Target>-->
</Project>

I've noticed that when I use MSDeploy I can see command that is executed during publish:

我注意到当我使用MSDeploy时,我可以看到在发布期间执行的命令:

"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:manifest='D:\GameBit\API\obj\Release\Package\API.SourceManifest.xml' -dest:auto,ComputerName="https://192.168.5.50:8172/msdeploy.axd?site=Default",UserName='LocalAdmin',Password="MyRealPassword",IncludeAcls='False',AuthType='Basic' -verb:sync -enableRule:EncryptWebConfig -enableRule:EncryptWebConfig -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:"D:\GameBit\API\obj\Release\Package\API.Publish.Parameters.xml" -allowUntrusted -retryAttempts=2 -userAgent="VS12.0:PublishDialog:WTE12.5.60612.0"

“C:\ Program Files(x86)\ IIS \ Microsoft Web Deploy V3 \ msdeploy.exe”-source:manifest ='D:\ GameBit \ API \ obj \ Release \ Package \ API.SourceManifest.xml' -dest:auto ,ComputerName =“https://192.168.5.50:8172/msdeploy.axd?site=Default”,UserName ='LocalAdmin',Password =“MyRealPassword”,IncludeAcls ='False',AuthType ='Basic'-verb:sync -enableRule:EncryptWebConfig -enableRule:EncryptWebConfig -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:“D:\ GameBit \ API \ obj \ Release \ Package \ API.Publish.Parameters.xml”-allowUntrusted -retryAttempts = 2 -userAgent =“VS12.0:PublishDialog:WTE12.5.60612.0”

Can I add -postSync:runCommand="" to that command from publish profile? As I found on MS site this parameter allows to execute command on destination machine.

我可以从发布配置文件中将-postSync:runCommand =“”添加到该命令吗?正如我在MS网站上发现的那样,该参数允许在目标机器上执行命令。

EDIT4:
I found information about Web Deploy Operation Settings and postSync setting, but I don't know where to set it, I don't want to edit Microsoft.Web.Publishing.targets from MSBuild folder

EDIT4:我找到了有关Web部署操作设置和postSync设置的信息,但我不知道在哪里设置它,我不想编辑MSBuild文件夹中的Microsoft.Web.Publishing.targets

I need to execute command on remote machine after publish succeeded.

发布成功后,我需要在远程机器上执行命令。

1 个解决方案

#1


6  

After going through your all edits and a bit of research from me , you want to execute the following command after the publish from the Visual Studio

在完成所有编辑和我的一些研究之后,您希望在从Visual Studio发布后执行以下命令

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pe "appSettings" -site Default -app "/" 

If i understood right , You can try wrapping the ItemGroup in a Target with AfterTargets set to AddIisSettingAndFileContentsToSourceManifest

如果我理解正确,您可以尝试将TargetGroup包装在Target中,并将AfterTargets设置为AddIisSettingAndFileContentsToSourceManifest

<Target Name="executeinHosts" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
    <ItemGroup>
      <MsDeploySourceManifest Include="runCommand">
         //here would be your path that need to run after the publish
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>

So in Your case this is how that part should look:

所以在你的情况下,这部分应该是这样的:

<Target Name="executeinHosts" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
    <ItemGroup>
      <MsDeploySourceManifest Include="runCommand">
         <path>C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pe "appSettings" -site $(DeployIisAppPath) -app "/"</path>
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>

Additional Info:

附加信息:

  • AddIisSettingAndFileContentsToSourceManifest target works justright before Web Deploy copying files from local to server.
  • AddIisSettingAndFileContentsToSourceManifest目标在Web部署将文件从本地复制到服务器之前工作justright。
  • aspnet_regiis can be run in <target> node by <Exec>.
  • aspnet_regiis可以通过 节点中运行。

Ex:

例如:

<Exec Command="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pef connectionStrings $(ProjectDir)obj\Debug\Package\PackageTmp" WorkingDirectory="$(publishUrl)" />

#1


6  

After going through your all edits and a bit of research from me , you want to execute the following command after the publish from the Visual Studio

在完成所有编辑和我的一些研究之后,您希望在从Visual Studio发布后执行以下命令

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pe "appSettings" -site Default -app "/" 

If i understood right , You can try wrapping the ItemGroup in a Target with AfterTargets set to AddIisSettingAndFileContentsToSourceManifest

如果我理解正确,您可以尝试将TargetGroup包装在Target中,并将AfterTargets设置为AddIisSettingAndFileContentsToSourceManifest

<Target Name="executeinHosts" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
    <ItemGroup>
      <MsDeploySourceManifest Include="runCommand">
         //here would be your path that need to run after the publish
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>

So in Your case this is how that part should look:

所以在你的情况下,这部分应该是这样的:

<Target Name="executeinHosts" AfterTargets="AddIisSettingAndFileContentsToSourceManifest">
    <ItemGroup>
      <MsDeploySourceManifest Include="runCommand">
         <path>C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis -pe "appSettings" -site $(DeployIisAppPath) -app "/"</path>
      </MsDeploySourceManifest>
    </ItemGroup>
  </Target>

Additional Info:

附加信息:

  • AddIisSettingAndFileContentsToSourceManifest target works justright before Web Deploy copying files from local to server.
  • AddIisSettingAndFileContentsToSourceManifest目标在Web部署将文件从本地复制到服务器之前工作justright。
  • aspnet_regiis can be run in <target> node by <Exec>.
  • aspnet_regiis可以通过 节点中运行。

Ex:

例如:

<Exec Command="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pef connectionStrings $(ProjectDir)obj\Debug\Package\PackageTmp" WorkingDirectory="$(publishUrl)" />