19

在我的 Web.Config 我有以下

  <system.webServer>

    <modules>
       **some code**
    </modules>
    <handlers>
       **some code**    
    </handlers>

  </system.webServer>

如何对其进行转换,以便将“安全”的新子部分注入“system.webServer”?到目前为止,我尝试和搜索的一切都失败了。

我想要的如下所示:

  <system.webServer>

    <modules>
       **some code**
    </modules>
    <handlers>
       **some code**    
    </handlers>

    <security>
      <ipSecurity allowUnlisted="false" denyAction="NotFound">
        <add allowed="true" ipAddress="10.148.176.10" />
      </ipSecurity>
    </security>

  </system.webServer>
4

2 回答 2

43

找到了有效的解决方案。在我的 Web.Azure.Config 文件中,我必须添加以下内容:

  <system.webServer>
    <security xdt:Transform="Insert">
      <ipSecurity allowUnlisted="false" denyAction="NotFound">
        <add allowed="true" ipAddress="10.148.176.10" />
      </ipSecurity>
    </security>
  </system.webServer>

我在发布问题之前尝试了这个,但由于 Web.Config 的另一部分中的拼写错误,它出错了。

于 2014-05-17T21:44:01.097 回答
0

正如您在答案中指定的那样,部署的最佳解决方案似乎是在 Web.Azure.Config 中指定。

只是为了好玩,发布这个 XSLT 解决方案,您也可以使用它来添加<security>带有 IP 地址的元素(如果它不存在),或者稍后调用以添加其他条目。ipAddress执行时在参数中设置IP地址。如果没有ipAddress指定,它什么也不做。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:param name="ipAddress"/>

   <xsl:template match="@*|node()">
       <xsl:copy>
           <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
   </xsl:template>

    <!--Create security/ipSecurity with specified IP address, 
        if specified in param-->
    <xsl:template match="system.webServer[not(security)]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:if test="$ipAddress">
                <security>
                    <ipSecurity allowUnlisted="false" denyAction="NotFound">
                        <add allowed="true" ipAddress="{$ipAddress}" />
                    </ipSecurity>
                </security>
            </xsl:if>
        </xsl:copy>      
    </xsl:template>

    <!--Add an allowed IP address to existing security/ipSecurity entry, 
        if IP address is specified in param -->
    <xsl:template match="security/ipSecurity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:if test="$ipAddress">
                <add allowed="true" ipAddress="{$ipAddress}" />
            </xsl:if>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
于 2014-05-18T00:57:42.377 回答