2014-05-17 2 views
8

나는변환을 사용하여 Web.Config에 새 섹션을 "삽입"할 수 있습니까? 내있는 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> 
+0

내가 아는 한 web.config는 응용 프로그램이 시작될 때만 읽을 수 있지만 응용 프로그램이 다시 시작될 때까지 적용되지는 않습니다. – Dalorzo

+0

'web.config'를 변경하면 앱이 다시 시작됩니다. 배포 중에는 변환을 통해 생성 할 수도 있지만 OP는 서버 자체를 기반으로 IP 주소를 사용하려고합니다. 그것은 XSLT와 아무 관련이 없지만 (호스팅 설정에 따라) 가능할 수도 있습니다. – harpo

+0

@harpo 명확히하기 위해 변형을 사용하여 배포 중에 섹션을 추가 할 수 있기를 원합니다. 건배 폴 –

답변

17

일한 해결책을 발견.

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

내가 그것을 erroring되었다의 Web.config의 다른 부분에 오타에 질문을 게시하기 전에하지만 때문에이 시도 : 내 Web.Azure.Config 파일 내에서 나는 다음과 같은 추가했다.

0

배포에 가장 적합한 솔루션은 답변에 지정된대로 Web.Azure.Config에 지정하는 것입니다.

<security> 요소를 IP 주소가 없으면 추가하거나 나중에 항목을 추가로 호출 할 때 사용할 수있는이 XSLT 솔루션을 게시하기 만하면됩니다. 실행시 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>