2014-05-17 15 views
8

Trong Web.Config của tôi, tôi đã sauCó cách nào để "chèn" một phần mới vào Web.Config bằng cách sử dụng biến đổi?

<system.webServer> 

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

    </system.webServer> 

Làm thế nào để chuyển đổi nó vì vậy tôi có thể tiêm một phần phụ mới cho "an ninh" vào "system.webServer"? Mọi thứ tôi đã thử và tìm kiếm cho đến nay đều thất bại.

Những gì tôi mong muốn được hiển thị dưới đây:

<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

Theo như tôi biết web.config chỉ được đọc một lần khi bắt đầu ứng dụng ngay cả khi bạn không thể có nghĩa là nó sẽ có hiệu lực cho đến khi khởi động lại ứng dụng. – Dalorzo

+0

Thay đổi 'web.config' sẽ kích hoạt khởi động lại ứng dụng. Tất nhiên bạn có thể tạo ra nó từ một biến đổi trong quá trình triển khai, nhưng có vẻ như OP muốn sử dụng một địa chỉ IP dựa trên chính máy chủ. Điều đó có thể là có thể (tùy thuộc vào thiết lập lưu trữ) mặc dù nó không có gì để làm với XSLT như vậy. – harpo

+0

@harpo Để làm rõ, tôi muốn có thể thêm phần trong khi triển khai bằng cách sử dụng các biến đổi. Chúc mừng Paul –

Trả lời

17

Tìm thấy các giải pháp mà làm việc. Trong tập tin Web.Azure.Config của tôi, tôi đã có thêm những điều sau đây:

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

tôi đã cố gắng này trước khi đăng câu hỏi nhưng do một lỗi đánh máy trong một phần khác của Web.Config nó được erroring.

0

Dường như giải pháp tốt nhất để triển khai sẽ được chỉ định trong Web.Azure.Config, như bạn đã chỉ định trong câu trả lời của mình.

Chỉ để giải trí, đăng giải pháp XSLT này mà bạn cũng có thể sử dụng để thêm phần tử <security> với địa chỉ IP nếu nó không tồn tại hoặc gọi sau để thêm các mục bổ sung. Đặt địa chỉ IP trong thông số ipAddress khi thực thi. Nếu không có ipAddress được chỉ định, nó sẽ không làm gì cả.

<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> 
Các vấn đề liên quan