HOW TO:配合 Internet Explorer 7.0 使用 Windows CardSpace

Download sample

CardSpace 提供使用者管理他們的數位身分識別的能力,而且搭配 Internet Explorer 7.0 後,網站可以向使用者要求數位身分識別。 下列練習示範使用 CardSpace 和 Internet Explorer 7.0 接受身分識別的步驟。 在本逐步解說中有四個練習:

  • Hello, World!

  • 視需要啟動身分識別選取器

  • 存取安全性權杖中的宣告

  • 唯一識別使用者

要使用練習必須設定網站。 使用範例資料夾提供的安裝批次檔來完成設定:

Setup.bat

如需安裝網站,以及一些疑難排解提示的詳細資訊,請參閱安裝 CardSpace 範例憑證

Hello World! 範例

Hello World 範例示範使用身分識別選取器所需的 HTML 程式碼。 本範例使用下列檔案:

sample1.htm

login1.aspx

使用 <object> 項目或「二進位行為物件」(Binary Behavior Object) 顯示身分識別選取器。 最簡單的方式就是將 <object> 項目加入 <form> 項目的本文中,在送出表單時啟動身分識別選取器。 下列是 Sample1.htm 檔案:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample 1</title>
</head>
<body>
    <form id="form1" method="post" action="login1.aspx">
      <button type="submit">Click here to sign in with your Information Card</button>
      <object type="application/x-informationcard" name="xmlToken">
        <param name="tokenType" value="urn:oasis:names:tc:SAML:1.0:assertion" />
        <param name="issuer" 
               value="https://schemas.xmlsoap.org/ws/2005/05/identity/issuer/self" />
        <param name="requiredClaims" 
             value="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname
                    https://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname
                    https://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
                    https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier" />
      </object>
    </form>
</body>
</html>

<object> 項目的結果直接張貼在發生處理所在的 login1.aspx 頁面上。 按下送出按鈕時,顯示身分識別選取器。

物件項目的重要項目有:

項目 描述

type="application/x-informationcard"

指示瀏覽器顯示身分識別選取器物件。

param name="tokenType"

控制身分識別選取器發出的權杖型別,在這個情況中是 SAML 1.0 權杖。

param name="issuer"

身分識別提供者的 URL 提供身分識別。 在這裡我們指的是叫用內建、自我發行提供者的內嵌式 URI。

param name="requiredClaims"

信賴憑證者要求使用者提供來自有特定宣告的身分識別提供者 (或使用者) 的權杖。 這裡的範例是自行發出 (個人的) 卡片的預先定義組合的一部分。

按下按鈕時,顯示 CardSpace 身分識別選取器。 這會允許使用者選擇要送出的 CardSpace 身分識別。

選擇 Windows CardSpce 卡片

按一下 [是,選擇傳送卡片]。 如果系統中沒有任何卡片,身分識別選取器會提供建立或匯入卡片的機會。

配合 Internet Explorer 7.0 使用 Windows CardSpace

按一下 [建立新的個人卡片],然後會顯示用於建立自行發出卡片的畫面。

編輯 Windows CardSpace 卡片

填寫必要的欄位,選擇卡片,為卡片提供名稱,然後按一下 [儲存]。 一建立滿足所列宣告的個人卡片時即傳送卡片。

傳送 Windows CardSpace 卡片至網站

加密的卡片資料傳送至下列的 Login1.aspx 頁面 (如以下在 C# 中所示)。

<%@ Page Language="C#"  Debug="true" ValidateRequest="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e) {
        Label1.Text = Request.Params["xmlToken"];
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        The value of the token is:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>

加密的卡片資料會傳送至 VB.net 中的 login1.aspx 頁面:

<%@ Page Language="VB"  Debug="true" ValidateRequest="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
   Label1.Text = Request.Params("xmlToken");
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        The value of the token is:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>

login1.aspx 頁面將加密的權杖顯示回畫面上。

配合 Internet Explorer 7.0 使用 Windows CardSpace

視需要啟動身分識別選取器

這個範例示範以更多彈性顯示身分識別選取器的方法。 範例使用下列檔案:

Sample2.htm

Login2.aspx

開發人員可能想要更多有關身分識別選取器的叫用時間和處理的彈性。 若要在更適合特定應用程式的時間顯示身分識別選取器,將 <object> 項目編寫為指令碼以傳回要求的加密權杖。 這在下列 Sample2.htm 檔案中顯示。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample 2</title>
  <object type="application/x-informationcard" name="_xmlToken">
    <param name="tokenType" value="urn:oasis:names:tc:SAML:1.0:assertion" />
    <param name="requiredClaims" 
        value="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname
        https://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname
        https://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
        https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier" />
  </object>
  <script language="javascript">
    function GoGetIt(){
      var xmltkn=document.getElementById("_xmltoken");
      var thetextarea = document.getElementById("xmltoken");
      thetextarea.value = xmltkn.value ;
    }
  </script>
</head>
<body>
  <form id="form1" method="post" action="login2.aspx">
    <button name="go" id="go" onclick="javascript:GoGetIt();">Click here to get the token.</button>
    <button type="submit">Click here to send the card to the server</button>
    <textarea cols=100 rows=20 id="xmltoken" name="xmlToken" ></textarea>
  </form>
</body>
</html>

<object> 項目放置在 HTML 文件的標頭中,並且在存取 value 屬性時叫用身分識別選取器。 這個範例中的指令碼將權杖 XML 放入 <textarea> 項目中,以允許開發人員在送出 <form> 之前檢視內容。

Note注意:

文字區中的權杖文字不會完全與 login2.aspx 頁面中的權杖文字相符,因為瀏覽器正在隱藏 XML 標記的顯示。

存取宣告

這個範例示範如何使用 TokenProcessor.cs,以存取在傳送至網站的卡片中的宣告。 範例使用下列檔案:

Sample3.htm

Login3.aspx

app_code\TokenProcessor.cs

Web.config

若要處理加密資料以外的宣告,使用 ASP.NET 2.0 的開發人員可以使用在此練習中提供的 TokenProcessor.cs 範例程式碼。 Token 類別使用 Windows Communication Foundation (WCF) 類別處理權杖的所有解密和驗證。 在張貼的目標頁面中使用 Token 類別,在這個案例中是 C# 中的 login3.aspx 頁面:

<%@ Page Language="C#"  Debug="true" ValidateRequest="false" %>
<%@ Import Namespace="System.IdentityModel.Claims" %>
<%@ Import Namespace="Microsoft.IdentityModel.TokenProcessor" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void ShowError(string text) {
        fields.Visible = false;
        errors.Visible = true;
        errtext.Text = text;
    }
    protected void Page_Load(object sender, EventArgs e)
        string xmlToken;
        xmlToken = Request.Params["xmlToken"];
        if (xmlToken == null || xmlToken.Equals("")){
            ShowError("Token presented was null");
        }
        else {
            Token token= new Token(xmlToken);
            givenname.Text = token.Claims[ClaimTypes.GivenName];
            surname.Text = token.Claims[ClaimTypes.Surname];
            email.Text = token.Claims[ClaimTypes.Email];
        }
        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div runat="server" id="fields">
        Given Name:<asp:Label ID="givenname" runat="server" Text=""></asp:Label><br/>
        Surname:<asp:Label ID="surname" runat="server" Text=""></asp:Label><br/>
        Email Address:<asp:Label ID="email" runat="server" Text=""></asp:Label><br/>
    </div>
    <div runat="server" id="errors" visible="false">
        Error:<asp:Label ID="errtext" runat="server" Text=""></asp:Label><br/>
    </div>
        
    </form>
</body>
</html>

在 Visual Basic .NET 中:

<%@ Page Language="VB"  Debug="true" ValidateRequest="false" %>
<%@ Import Namespace="System.IdentityModel.Claims" %>
<%@ Import Namespace="Microsoft.IdentityModel.TokenProcessor" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected  Sub ShowError(ByVal text As String) 
   fields.Visible = False 
   errors.Visible = True 
   errtext.Text = text 
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
   Dim xmlToken As String
   xmlToken = Request.Params("xmlToken")
   If xmlToken = Nothing Or xmlToken.Equals("") Then
      ShowError("Token presented was null")
   Else
      Dim token As New Token(xmlToken)
      givenname.Text = token.Claims(ClaimTypes.GivenName)
      surname.Text = token.Claims(ClaimTypes.Surname)
      email.Text = token.Claims(ClaimTypes.Email)
   End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div runat="server" id="fields">
        Given Name:<asp:Label ID="givenname" runat="server" Text=""></asp:Label><br/>
        Surname:<asp:Label ID="surname" runat="server" Text=""></asp:Label><br/>
        Email Address:<asp:Label ID="email" runat="server" Text=""></asp:Label><br/>
    </div>
    <div runat="server" id="errors" visible="false">
        Error:<asp:Label ID="errtext" runat="server" Text=""></asp:Label><br/>
    </div>
        
    </form>
</body>
</html>

若要取得權杖外的宣告值,請使用 Claims 屬性。 這會將宣告的 URI 視為參數,並且傳回做為 string 的值。 自行發出卡片的 URI 稍後在此文件中列出,但為了節省時間,請在 SelfIssued 類別中預先定義它們。

有搭配 Token 使用的組態資料。 在 Web.config 檔中,可以找到組態項目:

<configuration>
  <appSettings>
    <add key="MaximumClockSkew" value="60"/>
    <add key="CertifcateThumbprint" value="01234567890ABCDEFEDCBA01234567890ABCDEFEDCBA"/>
    <add key="StoreName" value="My"/>
    <add key="StoreLocation" value="LocalMachine"/>
    <add key="IdentityClaimType" 
         value="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier"/>
  </appSettings>
<configuration>
項目 描述

MaximumClockSkew

60

選擇項, 用戶端和伺服器可偏離的最大秒數。

CertificateThumbprint

<none>

選擇項, 用於解密權杖之憑證的指紋。 權杖自動查詢憑證。

StoreName

My

選擇項, SSL 憑證的存放區名稱。 通常是「My」。

StoreLocation

LocalMachine

選擇項, SSL 憑證的存放區位置。 通常是「LocalMachine」。

IdentityClaimType

PPID

選擇項, 宣告型別的 URI 當做唯一識別使用者的宣告使用。

權杖張貼至登入頁並解密後,我們可以看到宣告值。

配合 Internet Explorer 7.0 使用 Windows CardSpace

唯一識別使用者

這個範例示範如何使用 Token 類別來唯一識別使用者。 本範例使用下列檔案:

Sample4.htm

Login4.aspx

app_code\TokenProcessor.cs

web.config

如果任何人可以用於宣告並包含相同值的卡片,除了自我發行的宣告值外,必須能唯一識別特定卡片。這可藉由計算卡片的 UniqueID 達成。 這個 UniqueID 是發行者的公開金鑰和該發行者的任何唯一宣告的雜湊。由於用戶端和伺服器之間的對話金鑰是第一次通訊時隨機產生,因此公用金鑰是卡片的唯一金鑰—如果您在多個網站上使用相同的自行發出卡片,每個新關聯都會產生新的金鑰組合。 對於唯一宣告而言,卡片的「私密個人識別碼」(Private Personal Identifier,PPID) 是該發行者的唯一識別碼。 UniqueID 屬性計算開發人員的雜湊值。 將程式碼檔案新增至 C# 中的 login4.aspx 頁面:

<%@ Page Language="C#"  Debug="true" ValidateRequest="false" %>
<%@ Import Namespace="System.IdentityModel.Claims" %>
<%@ Import Namespace="Microsoft.IdentityModel.TokenProcessor" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void ShowError(string text) {
        fields.Visible = false;
        errors.Visible = true;
        errtext.Text = text;
    }
    protected void Page_Load(object sender, EventArgs e) {
        string xmlToken;
        xmlToken = Request.Params["xmlToken"];
        if (xmlToken == null || xmlToken.Equals(""))
            ShowError("Token presented was null");
        else
        {
                Token token = new Token (xmlToken);
                givenname.Text = token.Claims[SelfIssued.GivenName];
                surname.Text = token.Claims[SelfIssued.Surname];
                email.Text = token.Claims[SelfIssued.EmailAddress];
                uid.Text = token.UniqueID;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div runat="server" id="fields">
        Given Name:<asp:Label ID="givenname" runat="server" Text=""></asp:Label><br/>
        Surname:<asp:Label ID="surname" runat="server" Text=""></asp:Label><br/>
        Email Address:<asp:Label ID="email" runat="server" Text=""></asp:Label><br/>
        Unique ID:<asp:Label ID="uid" runat="server" Text=""></asp:Label><br/>
    </div>
    <div runat="server" id="errors" visible="false">
        Error:<asp:Label ID="errtext" runat="server" Text=""></asp:Label><br/>
    </div>
        
    </form>
</body>
</html>

在 Visual Basic .NET 中:

<%@ Page Language="VB"  Debug="true" ValidateRequest="false" %>
<%@ Import Namespace="System.IdentityModel.Claims" %>
<%@ Import Namespace="Microsoft.IdentityModel.TokenProcessor" %>

<script runat="server">
Protected  Sub ShowError(ByVal text As String) 
   fields.Visible = False 
   errors.Visible = True 
   errtext.Text = text 
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
   Dim xmlToken As String
   xmlToken = Request.Params("xmlToken")
   If xmlToken = Nothing Or xmlToken.Equals("") Then
      ShowError("Token presented was null")
   Else
      Dim token As New Token(xmlToken)
      givenname.Text = token.Claims(ClaimTypes.GivenName)
      surname.Text = token.Claims(ClaimTypes.Surname)
      email.Text = token.Claims(ClaimTypes.Email)
      uid.Text = token.UniqueID
   End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div runat="server" id="fields">
        Given Name:<asp:Label ID="givenname" runat="server" Text=""></asp:Label><br/>
        Surname:<asp:Label ID="surname" runat="server" Text=""></asp:Label><br/>
        Email Address:<asp:Label ID="email" runat="server" Text=""></asp:Label><br/>
        Unique ID:<asp:Label ID="uid" runat="server" Text=""></asp:Label><br/>
    </div>
    <div runat="server" id="errors" visible="false">
        Error:<asp:Label ID="errtext" runat="server" Text=""></asp:Label><br/>
    </div>
        
    </form>
</body>
</html>

結果是可在資料庫中用來唯一識別訪客的一個 20 個字元、Base 64 編碼的雜湊值。

配合 Internet Explorer 7.0 使用 Windows CardSpace

若要變更唯一使用的宣告,請將下列金鑰新增至 Web.config,然後將值變更為不同的宣告 URI。

<add key="IdentityClaimType" value="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier"/>

關於實際權杖

如需加密的 XML 權杖的格式和取用的詳細資訊,請查詢下列資料來源:

https://go.microsoft.com/fwlink/?LinkId=95951

自行發出的卡片中的宣告

  • 指定名稱 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname";

  • 電子郵件地址 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress";

  • 姓氏 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname";

  • 街道地址 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/streetaddress";

  • 位置 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality";

  • 州/省 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/stateorprovince";

  • 郵遞區號 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalcode";

  • 國家 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/country";

  • 住家電話號碼 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/homephone";

  • 其他電話 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/otherphone";

  • 行動電話 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/mobilephone";

  • 生日 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth";

  • 性別 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender";

  • PPID = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier";

  • 網站 = "https://schemas.xmlsoap.org/ws/2005/05/identity/claims/website";

字彙

  • 身分識別提供者 – 主張有關主體 (人員) 的宣告的代理者 (公司、網站、組織、您自己)。

  • 身分識別選取器 – 身分識別選取器是顯示使用者不同的數位身分識別的對話,並允許選擇一個相關的身分識別以送出給信賴憑證者。

  • 信賴憑證者 – 依據主體提供之宣告的代理者 (網站、伺服器或其他方)。

  • 主體 – 實體 (使用者) 對於他們的身分識別做的宣告。主體永遠控制其原始身分識別資訊對信賴憑證者的發行。

Footer image

請將您對這個主題的意見傳送至 Microsoft。

Copyright © 2007 by Microsoft Corporation. All rights reserved.