2016-12-15 97 views
0

我有一個Azure的Web應用程序配置爲用於非PROD環境中的web.config基本身份驗證,如下圖所示:Azure Web App - 通過applicationHost.xdt添加基本身份驗證?

<configSections> 
      <section name="basicAuth" type="Devbridge.BasicAuthentication.Configuration.BasicAuthenticationConfigurationSection" /> 
     </configSections> 
     <basicAuth allowRedirects="true"> 
      <credentials> 
       <add username="username" password="password"/> 
      </credentials> 
     </basicAuth> 
     <system.webServer> 
      <modules> 
       <add name="MyBasicAuthenticationModule" type="Devbridge.BasicAuthentication.BasicAuthenticationModule"/> 
      </modules> 
<!-- the rest of the web.config follows --> 

一切工作正常,但每當我們做了部署PROD與改變web.config中,需要手動更改文件才能禁用基本身份驗證(如上所述,我們僅在非prod上需要它)。

所以我想知道 - 有沒有辦法使用applicationHost.xdt文件進行基本身份驗證?由於這是一個不經常更改的文件,它會使我們的生活更輕鬆。

我已經檢查IIS管理器擴展,但沒有看到任何東西,讓我來完成這項工作。任何提示都表示讚賞!

更新 - 加入我的web.config(我想和applicationHost.xdt更新)

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <system.webServer> 
     <rewrite> 
      <rules> 
       <rule name="Redirect to HTTPS" stopProcessing="true"> 
        <match url="(.*)"/> 
        <conditions> 
         <add input="{HTTPS}" pattern="^OFF$"/> 
        </conditions> 
        <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/> 
       </rule> 
      </rules> 
     </rewrite> 
    </system.webServer> 
</configuration> 

回答

1

根據你的描述,我假設你正在使用DevBridge Azure Power Tools支持基本認證Windows Azure網站。我跟着這個項目Devbridge.BasicAuthentication.Test在我身邊測試XDT轉換。我可以讓它在我身邊工作,你可以參考它。

1.創建一個Release-dev配置

點擊「生成>配置管理器」,添加對Web項目的新配置。

2.添加一個Web配置文件命名Web.Release-dev.config和配置內容如下:

<?xml version="1.0"?> 
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 

    <!--make sure the configSections is the first child element under configuration--> 
    <configSections xdt:Transform="InsertBefore(/configuration/*[1])" /> 
    <configSections xdt:Locator="XPath(/configuration/configSections[last()])"> 
    <section name="basicAuth" type="Devbridge.BasicAuthentication.Configuration.BasicAuthenticationConfigurationSection" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/> 
    </configSections> 
    <configSections xdt:Transform="RemoveAll" xdt:Locator="Condition(count(*)=0)" /> 

    <basicAuth allowRedirects="true" xdt:Transform="InsertAfter(/configuration/configSections)"> 
    <credentials xdt:Transform="InsertIfMissing"> 
     <add username="test" password="test" xdt:Transform="InsertIfMissing"/> 
    </credentials> 
    </basicAuth> 

    <system.webServer xdt:Transform="InsertIfMissing"> 
    <modules xdt:Transform="InsertIfMissing"> 
     <add name="MyBasicAuthenticationModule" type="Devbridge.BasicAuthentication.BasicAuthenticationModule" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/> 
    </modules> 
    </system.webServer> 

</configuration> 

注:您可以參考Xdt transform samples。此外,您可以關注在您的Web.config轉換文件中使用的xdt:Transformxdt:Locator屬性的語法的官方document

3.Publish使用release-dev配置Web項目:通過KUDU

4,檢查部署的web.config文件:

瀏覽器的網站,你可以看到下面的截圖:

UPDATE

有關解決方法,我認爲你可以從你的git倉庫排除web.config文件。並添加網頁。在配置文件 「d:\家\網站\ wwwroot文件」 下,並Devbridge.BasicAuthentication.dll 「d:\家\網站\ wwwroot的\ BIN」 爲您DEV和QA環境,使基本身份驗證如下:

+0

謝謝。我嘗試部署提到applicationHost.xdt(順便說一句,這是一個PHP應用程序,而不是.NET,我們不使用VS,但直接與GitHub集成進行部署)。我收到下面的錯誤 - 「Config section'system.webServer/modules'already defined。Sections must only only once once per config file。」,但我沒有在web.config中定義模塊部分(請參閱q)。 –

+0

忽略我對你的情況的誤解。根據您最近更新的內容,您是否意味着您正試圖在'applicationHost.xdt'文件中添加URL重寫規則以禁用基本身份驗證?如果是這樣,有一個關於[重定向http流量到https]的xdt轉換示例(https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples),您可以參考它。 –

+0

此外,您可以在網絡應用程序的「開發工具」>「擴展」刀片下的Azure門戶上安裝名爲「將HTTP重定向至HTTPS」的站點擴展,或安裝[將HTTP重定向至HTTPS](http://www.siteextensions .net/packages/RedirectHttpToHttps /)擴展名通過KUDU。 –