从Visual Studio发布并使用aspnet_regiis

时间:2016-08-01 07:24:09

标签: asp.net visual-studio encryption visual-studio-2013 microsoft-web-deploy

我有部署到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&gt; Publish)部署网站,并在发布成功后自动执行aspnet_regiis命令。

found information我可以使用runcommandother about bat files,但我没有从命令行调用MSDeploy。
我还发现了我应该构建自定义提供程序并从MSDeploy调用它的信息。

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

EDIT1:
我设法使用以下方式挂钩After Deploy目标:

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

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

  

命令&#34; aspnet_regiis -pe&#34; appSettings&#34; -site Default -app&#34; /&#34;&#34;   退出代码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>

但我没有运气。 I found blog关于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时,我可以看到在发布期间执行的命令:

  

&#34; C:\ Program Files(x86)\ IIS \ Microsoft Web Deploy V3 \ msdeploy.exe&#34;   -source:清单=&#39; d:\ GameBit \ API \ OBJ \发布\包装\ API.SourceManifest.xml&#39;   -dest:汽车,计算机名=&#34; HTTPS://192.168.5.50:8172 / msdeploy.axd站点=默认&#34;,用户名=&#39;为localadmin&#39;,密码=&#34;&MyRealPassword #34;,IncludeAcls =&#39;假&#39;,=进行AuthType&#39;具有基本&#39;   -verb:sync -enableRule:EncryptWebConfig -enableRule:EncryptWebConfig -disableLink:AppPoolExtension -disableLink:ContentExtension -disableLink:CertificateExtension -setParamFile:&#34; D:\ GameBit \ API \ obj \ Release \ Package \ API.Publish.Parameters。 XML&#34;   -allowUntrusted -retryAttempts = 2 -userAgent =&#34; VS12.0:PublishDialog:WTE12.5.60612.0&#34;

我可以从发布配置文件中将-postSync:runCommand=""添加到该命令吗?如I found on MS site,此参数允许在目标计算机上执行命令。

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

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

1 个答案:

答案 0 :(得分:10)

在完成所有编辑和我的一些研究后,您希望在从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部署将文件从本地复制到服务器之前工作。
  • aspnet_regiis可以<target><Exec>节点中运行。

实施例

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