共用方式為


系結系結 <>

概觀

專案的 <binding> 元素 <bindings> 可讓您設定要求與網站通訊所需的資訊。

您可以在建立網站時設定系結資訊,也可以在建立網站之後編輯系結資訊。 系結資訊包括用戶端用來與月臺通訊的通訊協定、月臺的 IP 位址、埠號碼和主機標頭。

元素 <binding> 包含兩個屬性來設定系結資訊: bindingInformation通訊協定bindingInformation屬性包含 IP 位址、埠號碼,以及月臺的主機標頭。 通訊 協定 屬性會定義用來與月臺通訊的通訊協定。

您可以使用 IIS 管理員設定月臺的系結集合,方法是選取網站並使用 [動作] 窗格中的 [系結...]或 [進階設定...]連結,或在網站的快捷方式功能表上按一下 [編輯系結...]。

IIS 8.0 或更新版本可讓您使用 Windows Server 2012 上集中式憑證存放區中的憑證,為網站提供安全性。 如需詳細資訊,請參閱 IIS 8.0 集中式 SSL 憑證支援:SSL 延展性和管理性

綁定項可讓您指定用於安全通訊端層 (SSL) 憑證的系結類型。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <binding>
IIS 8.5 未在 IIS 8.5 中修改專案 <binding>
IIS 8.0 sslFlags已新增 屬性以指定用於安全通訊端層憑證的系結。
IIS 7.5 未在 IIS 7.5 中修改專案 <binding>
IIS 7.0 <binding>集合的 <bindings> 元素是在 IIS 7.0 中引進。
IIS 6.0 集合 <bindings> 會取代 IIS 6.0 IIsWebServer Metabase 物件上ServerBindings屬性的區段。

安裝程式

元素 <binding> 包含在 IIS 7 和更新版本的預設安裝中。

如何安裝集中式憑證存放區

在 IIS 8 和更新版本中,集中式憑證存放區中的憑證可用來提供網站的安全性。 若要在 Windows Server 2012 或更新版本的 Web 服務器上使用此存放區,您必須安裝集中式 SSL 憑證存儲角色。 若要在已安裝 IIS 時執行此動作,請繼續進行,如下所示:

  1. Windows 鍵,然後按一下[伺服器管理員]。
  2. 伺服器管理員中,按一下 [管理],然後按一下 [新增角色及功能]。
  3. 在 [新增角色及功能精靈] 中,按一下 [開始之前] 頁面,選取安裝類型,然後按一下 [安裝類型] 頁面上的 [下一步],然後選取要安裝的位置,然後按一下[伺服器選取] 頁面上的 [下一步]。
  4. 在 [ 伺服器角色] 頁面中,確定已選取 [Web 服務器 (IIS) ],然後展開它。 展開 [Web 服務器],然後選取並展開 [ 安全性]。
  5. 選取 [集中式 SSL 憑證存儲],然後按 [ 下一步]。
  6. 在 [ 功能] 頁面上,按 [ 下一步]。
  7. 在 [確認安裝選項] 頁面上,按一下 [安裝]
  8. 在 [結果] 頁面上,按一下 [關閉]

作法

如何將系結資訊新增至網站

  1. 開啟 [Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager
  2. 在 [ 連線] 窗格中,展開伺服器名稱、展開 [網站],然後按一下您要設定系結的網站。

  3. 在 [ 動作 ] 窗格中,按一下 [ 系結...

  4. 在 [ 網站系結 ] 對話方塊中,按一下 [ 新增...]
    [網站系結] 對話方塊的螢幕擷取畫面。

  5. 在 [ 新增網站系結 ] 對話方塊中,新增系結資訊,然後按一下 [ 確定]。
    [新增網站系結] 對話方塊的螢幕擷取畫面。

如何建立集中式憑證存放區

  1. 按 [開始]按鈕,然後按一下[伺服器管理員]。 在伺服器管理員中,按一下 [工具],然後按一下 [Internet Information Services (IIS) 管理員]。
  2. 在 [ 連線] 窗格中,選取伺服器名稱。
  3. 在 [ 首頁 ] 的 [ 管理 ] 區域底下,按兩下 [ 集中式憑證]。
  4. 在 [ 動作] 窗格中,按一下 [ 編輯功能設定]。
  5. 選取 [啟用集中式憑證 ],然後輸入實體路徑。 輸入路徑的使用者名稱、輸入路徑的密碼,並確認密碼。 按一下 [確定]。

組態

您可以在ApplicationHost.config檔案中每個網站的 元素內 <bindings> 新增 <binding> 元素。 每個網站都必須至少有一個 HTTP 或 HTTPS 系結,才能透過網際網路檢視。

屬性

屬性 描述
bindingInformation 必要的字串屬性。

指定要與月臺通訊的資訊。 例如,網站系結包含 IP 位址 (或未指定的 IP 位址) 、埠號碼,以及用來與網站通訊的選擇性主機標頭。
protocol 必要的字串屬性。

指定與月臺通訊的通訊協定。
sslFlags 選擇性 uint 屬性。

指定用於安全通訊端層 (SSL) 憑證的系結類型。
  • 值為 「0」 會指定使用 IP/埠組合建立安全連線。 只有一個憑證可以系結至 IP 位址和埠的組合。
  • 值為 「1」 會指定使用埠號碼進行安全連線,並使用伺服器名稱指示 (SNI) 取得的主機名稱。
  • 值為 「2」 會指定使用集中式 SSL 憑證存儲建立安全連線,而不需要伺服器名稱指標。
  • 值 「3」 指定使用集中式 SSL 憑證存儲建立安全連線,同時需要伺服器名稱指標
集中式 SSL 憑證支援可讓您建立可包含多個憑證檔案的集中式憑證存放區。 您可以命名憑證檔案,以對應至它們所包含的主機名稱。 這可讓您建立只需要端口的系結,而不是 IP/埠或主機名稱/埠組合。 當要求傳入時,IIS 會比對埠、從要求判斷主機名稱,然後搜尋集中式憑證存放區中是否有具有相符名稱的憑證檔案。 它會使用該憑證。

使用伺服器名稱指標 (SNI) ,主機名稱會交換為 SSL 交握的一部分。 當您新增具有 HTTPS 類型的系結時,會在 [新增網站系結] 對話方塊中啟用 SNI。 這對於在單一網路位址上裝載多部伺服器的 SSL 連線特別有用。 如需詳細資訊,請參閱 IIS 8.0 伺服器名稱指示 (SNI) :SSL 延展性

只有在通訊協定為 HTTPs時,才會設定 sslFlags 屬性。 預設值是 0

子元素

無。

組態範例

下列範例會定義名為 Contoso 的網站,其中包含兩個系結。 第一個系結適用于埠 80 上 「www.contoso.com」 的主機名稱,IP 位址為 192.168.0.1,而第二個系結則適用于透過埠 443 的所有 IP 位址的 HTTPS 系結。

<site name="Contoso" id="2">
   <application path="/" applicationPool="Contoso">
      <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot" />
   </application>
   <bindings>
      <binding protocol="http" bindingInformation="192.168.0.1:80:www.contoso.com" />
      <binding protocol="https" bindingInformation="*:443:" />
   </bindings>
</site>

範例程式碼

下列範例會針對埠 192.168.0.1 的 IP 位址,設定名為 Contoso 的網站,其主機名稱為 「www.contoso.com」,以及埠 443 上所有 IP 位址的 HTTPS 系結。

AppCmd.exe

appcmd.exe set site /site.name:Contoso /+bindings.[protocol='http',bindingInformation='192.168.0.1:80:www.contoso.com']

appcmd.exe set site /site.name:Contoso /+bindings.[protocol='https',bindingInformation='*:443:']

或者,您可以使用:

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].bindings.[protocol='http',bindingInformation='192.168.0.1:80:www.contoso.com']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].bindings.[protocol='https',bindingInformation='*:443:']" /commit:apphost

注意

當您使用AppCmd.exe設定這些設定時,請務必將 認可 參數 apphost 設定為 。 這會將組態設定認可至ApplicationHost.config檔案中適當的位置區段。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
          Configuration config = serverManager.GetApplicationHostConfiguration();
          ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
          ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
          ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Contoso");

          if (siteElement == null) throw new InvalidOperationException("Element not found!");

          ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings");
          ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
          bindingElement["protocol"] = @"http";
          bindingElement["bindingInformation"] = @"192.168.0.1:80:www.contoso.com";
          bindingsCollection.Add(bindingElement);

          ConfigurationElement bindingElement1 = bindingsCollection.CreateElement("binding");
          bindingElement1["protocol"] = @"https";
          bindingElement1["bindingInformation"] = @"*:443:";
          bindingsCollection.Add(bindingElement1);

          serverManager.CommitChanges();
      }
   }

   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
   {
      foreach (ConfigurationElement element in collection)
      {
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
         {
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
            {
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
               {
                  value = o.ToString();
               }
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
               {
                  matches = false;
                  break;
               }
            }
            if (matches)
            {
               return element;
            }
         }
      }
      return null;
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Contoso")

      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")

      Dim bindingElement As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement("protocol") = "http"
      bindingElement("bindingInformation") = "192.168.0.1:80:www.contoso.com"
      bindingsCollection.Add(bindingElement)

      Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement1("protocol") = "https"
      bindingElement1("bindingInformation") = "*:443:"
      bindingsCollection.Add(bindingElement1)

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function


End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Contoso"]);

if (siteElementPos == -1) throw "Element not found!";

var siteElement = sitesCollection.Item(siteElementPos);
var bindingsCollection = siteElement.ChildElements.Item("bindings").Collection;

var bindingElement = bindingsCollection.CreateNewElement("binding");
bindingElement.Properties.Item("protocol").Value = "http";
bindingElement.Properties.Item("bindingInformation").Value = "192.168.0.1:80:www.contoso.com";
bindingsCollection.AddElement(bindingElement);

var bindingElement1 = bindingsCollection.CreateNewElement("binding");
bindingElement1.Properties.Item("protocol").Value = "https";
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:";
bindingsCollection.AddElement(bindingElement1);

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Contoso"))

If siteElementPos = -1 Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If

Set siteElement = sitesCollection.Item(siteElementPos)
Set bindingsCollection = siteElement.ChildElements.Item("bindings").Collection

Set bindingElement = bindingsCollection.CreateNewElement("binding")
bindingElement.Properties.Item("protocol").Value = "http"
bindingElement.Properties.Item("bindingInformation").Value = "192.168.0.1:80:www.contoso.com"
bindingsCollection.AddElement(bindingElement)

Set bindingElement1 = bindingsCollection.CreateNewElement("binding")
bindingElement1.Properties.Item("protocol").Value = "https"
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:"
bindingsCollection.AddElement(bindingElement1)

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function