繁体   English   中英

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

[英]Publish from Visual Studio and automatically encrypt appSettings using aspnet_regiis

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

<MSDeployEnableWebConfigEncryptRule>true</MSDeployEnableWebConfigEncryptRule>

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

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

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

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

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

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

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>

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

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

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>

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

EDIT3:
我在下面添加我的发布配置文件:

<?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>

我注意到当我使用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”

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

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

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

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

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

如果我理解正确的,你可以尝试包裹ItemGroup与在目标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>

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

<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>

附加信息:

  • AddIisSettingAndFileContentsToSourceManifest目标在Web Deploy将文件从本地复制到服务器之前工作justright。
  • aspnet_regiis可以通过<Exec><target>节点中运行。

例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM