共用方式為


在應用程式之間共用憑證

需要使用者標識碼和密碼組合以外的安全驗證的通用 Windows 平臺 (UWP) 應用程式可以使用憑證進行驗證。 驗證使用者時,憑證驗證可提供高度的信任。 在某些情況下,服務群組會想要驗證多個應用程式的使用者。 本文說明如何使用相同的憑證來驗證多個應用程式,以及如何為使用者提供方便的程式代碼匯入提供的憑證,以存取受保護的Web服務。

應用程式可以使用憑證向 Web 服務進行驗證,而多個應用程式可以使用憑證儲存的單一憑證來驗證相同的使用者。 如果商店中不存在憑證,您可以將程式代碼新增至您的應用程式,以從 PFX 檔案匯入憑證。

啟用 Microsoft Internet Information Services(IIS)和用戶端憑證映射

本文以 Microsoft Internet Information Services(IIS)作為示例。 預設不會啟用 IIS。 您可以使用 [控制面板] 來啟用 IIS。

  1. 開啟 [控制面板],然後選取 [程式]
  2. 選取 [開啟或關閉 Windows 功能
  3. 展開 Internet Information Services,然後展開 World Wide Web Services。 展開 [應用程式開發功能 ],然後選取 [ASP.NET 3.5ASP.NET 4.5]。 進行這些選擇會自動啟用 Internet Information Services
  4. 確定 套用變更。

建立並發佈安全的 Web 服務

  1. 以系統管理員身分執行 Microsoft Visual Studio,然後從起始頁面選取 [ 新增專案 ]。 需要系統管理員存取權,才能將 Web 服務發佈至 IIS 伺服器。 在 [新增專案] 對話框中,將架構變更為 .NET Framework 3.5。 選取 Visual C# -> ->Visual Studio ->ASP.NET Web 服務應用程式。 將應用程式命名為 「FirstContosoBank」。 按一下 [確定] 來建立專案。

  2. Service1.asmx.cs 檔案中,將預設 HelloWorld Web 方法取代為下列 「Login」 方法。

            [WebMethod]
            public string Login()
            {
                // Verify certificate with CA
                var cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(
                    this.Context.Request.ClientCertificate.Certificate);
                bool test = cert.Verify();
                return test.ToString();
            }
    
  3. 儲存 Service1.asmx.cs 檔案。

  4. 在 [方案總管]中,以滑鼠右鍵按兩下 “FirstContosoBank” 應用程式,然後選取 [發佈]

  5. 在 [發佈 Web] 對話框中,建立新的設定檔並將其命名為「ContosoProfile」。 按 [下一步]

  6. 在下一個頁面上,輸入 IIS 伺服器的伺服器名稱,並指定網站名稱 「Default Web Site/FirstContosoBank」。。 按一下 發佈 來發佈您的網頁服務。

設定 Web 服務以使用用戶端憑證驗證

  1. 執行 Internet Information Services (IIS) 管理器
  2. 展開 IIS 伺服器的網站。 在 [默認網站] 底下,選取新的 “FirstContosoBank” Web 服務。 在 [ 動作] 區段中,選取 [ 進階設定...]。
  3. 應用程式集區 設定為 .NET v2.0,然後按一下 確定
  4. [Internet Information Services (IIS) 管理員] 中,選取 IIS 伺服器,然後按兩下 [伺服器憑證]。 在 [動作] 區段中,選取 [建立 Self-Signed 憑證...]。輸入 "ContosoBank" 作為憑證的易記名稱,按一下 [確定]。 這會建立新的憑證,以供 IIS 伺服器使用,形式為「<伺服器名稱>.<網域名稱>」。
  5. Internet Information Services (IIS) 管理員中,選取預設的網站。 在 [動作] 區段中,選取 [系結],然後按一下 [新增...]。選取「https」作為類型,將埠設定為「443」,然後輸入 IIS 伺服器的完整主機名稱("<伺服器名稱>.<功能變數名稱>")。 將 SSL 憑證設定為 「ContosoBank」。。 按一下 [確定]。 按一下 [關閉],在 [網站系結] 視窗中。
  6. Internet Information Services (IIS) 管理員中,選取 “FirstContosoBank” Web 服務。 按兩下 [SSL 設定]。 檢查 需要 SSL。 在 [客戶端憑證] 中,選取 [需要]。 在 [動作] 區段中,按一下 [套用]。
  7. 您可以開啟網頁瀏覽器並輸入下列網址,以確認網頁服務已正確設定:「https://<伺服器名稱>.<網域名稱>/FirstContosoBank/Service1.asmx」。 例如,“https://myserver.example.com/FirstContosoBank/Service1.asmx"。 如果您的 Web 服務已正確設定,系統會提示您選取客戶端憑證以存取 Web 服務。

您可以重複上述步驟,以建立多個 Web 服務,以使用相同的用戶端憑證來存取。

建立使用憑證驗證的 UWP 應用程式

現在您已經擁有一個或多個安全的 Web 服務,您的應用程式可以使用憑證對這些 Web 服務進行驗證。 當您使用 HttpClient 物件向已驗證的 Web 服務提出要求時,初始要求將不會包含客戶端憑證。 經過驗證的 Web 服務將回應用戶端驗證要求。 發生這種情況時,Windows 用戶端將自動查詢憑證儲存以取得可用的用戶端憑證。 您的使用者可以從這些憑證中進行選擇,以對 Web 服務進行驗證。 有些憑證受密碼保護,因此您需要為使用者提供輸入憑證密碼的方法。

如果沒有可用的用戶端憑證,則使用者需要將憑證新增至憑證儲存。 您可以在應用程式中包含程式碼,使使用者能夠選擇包含用戶端憑證的 PFX 檔案,然後將該憑證匯入到用戶端憑證存放區中。

提示 您可以使用 makecert.exe 來建立用於此快速入門的 PFX 檔案。 如需使用 makecert.exe的詳細資訊,請參閱 MakeCert。

 

  1. 開啟 Visual Studio,然後從起始頁面建立新的專案。 將新專案命名為「FirstContosoBankApp」。 按一下 [確定] 以建立新的專案。

  2. 在MainPage.xaml檔案中,將下列 XAML 新增至預設 Grid 元素。 此XAML 包括一個用於瀏覽要匯入的PFX 檔案的按鈕、一個用於為受密碼保護的PFX 檔案輸入密碼的文字方塊、一個用於匯入所選PFX 檔案的按鈕、一個用於登入安全 Web 服務的按鈕,以及顯示目前動作狀態的文字區塊。

    <Button x:Name="Import" Content="Import Certificate (PFX file)" HorizontalAlignment="Left" Margin="352,305,0,0" VerticalAlignment="Top" Height="77" Width="260" Click="Import_Click" FontSize="16"/>
    <Button x:Name="Login" Content="Login" HorizontalAlignment="Left" Margin="611,305,0,0" VerticalAlignment="Top" Height="75" Width="240" Click="Login_Click" FontSize="16"/>
    <TextBlock x:Name="Result" HorizontalAlignment="Left" Margin="355,398,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="153" Width="560"/>
    <PasswordBox x:Name="PfxPassword" HorizontalAlignment="Left" Margin="483,271,0,0" VerticalAlignment="Top" Width="229"/>
    <TextBlock HorizontalAlignment="Left" Margin="355,271,0,0" TextWrapping="Wrap" Text="PFX password" VerticalAlignment="Top" FontSize="18" Height="32" Width="123"/>
    <Button x:Name="Browse" Content="Browse for PFX file" HorizontalAlignment="Left" Margin="352,189,0,0" VerticalAlignment="Top" Click="Browse_Click" Width="499" Height="68" FontSize="16"/>
    <TextBlock HorizontalAlignment="Left" Margin="717,271,0,0" TextWrapping="Wrap" Text="(Optional)" VerticalAlignment="Top" Height="32" Width="83" FontSize="16"/>
    
  3. 儲存MainPage.xaml 檔案。

  4. 在 MainPage.xaml.cs 檔案中,新增下列 using 語句。

    using Windows.Web.Http;
    using System.Text;
    using Windows.Security.Cryptography.Certificates;
    using Windows.Storage.Pickers;
    using Windows.Storage;
    using Windows.Storage.Streams;
    
  5. 在 MainPage.xaml.cs 檔案中,將下列變數新增至 MainPage 類別。 他們會指定用於您「FirstContosoBank」Web 服務的安全的「登入」方法的位址,以及用於保存要匯入到憑證存儲區的 PFX 憑證的全域變數。 將 <伺服器名稱> 更新為 Microsoft Internet Information Server (IIS) 伺服器的完整伺服器名稱。

    private Uri requestUri = new Uri("https://<server-name>/FirstContosoBank/Service1.asmx?op=Login");
    private string pfxCert = null;
    
  6. 在MainPage.xaml.cs檔案中,為登入按鈕和方法來新增下列點選處理程式,以存取受保護的Web服務。

    private void Login_Click(object sender, RoutedEventArgs e)
    {
        MakeHttpsCall();
    }
    
    private async void MakeHttpsCall()
    {
    
        StringBuilder result = new StringBuilder("Login ");
        HttpResponseMessage response;
        try
        {
            Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
            response = await httpClient.GetAsync(requestUri);
            if (response.StatusCode == HttpStatusCode.Ok)
            {
                result.Append("successful");
            }
            else
            {
                result = result.Append("failed with ");
                result = result.Append(response.StatusCode);
            }
        }
        catch (Exception ex)
        {
            result = result.Append("failed with ");
            result = result.Append(ex.Message);
        }
    
        Result.Text = result.ToString();
    }
    
  7. 在 MainPage.xaml.cs 檔案中,為按鈕新增下列點擊事件處理程序,一個是用來瀏覽 PFX 檔案,另一個是用來將選取的 PFX 檔案匯入證書儲存區的按鈕。

    private async void Import_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Result.Text = "Importing selected certificate into user certificate store....";
            await CertificateEnrollmentManager.UserCertificateEnrollmentManager.ImportPfxDataAsync(
                pfxCert,
                PfxPassword.Password,
                ExportOption.Exportable,
                KeyProtectionLevel.NoConsent,
                InstallOptions.DeleteExpired,
                "Import Pfx");
    
            Result.Text = "Certificate import succeeded";
        }
        catch (Exception ex)
        {
            Result.Text = "Certificate import failed with " + ex.Message;
        }
    }
    
    private async void Browse_Click(object sender, RoutedEventArgs e)
    {
    
        StringBuilder result = new StringBuilder("Pfx file selection ");
        FileOpenPicker pfxFilePicker = new FileOpenPicker();
        pfxFilePicker.FileTypeFilter.Add(".pfx");
        pfxFilePicker.CommitButtonText = "Open";
        try
        {
            StorageFile pfxFile = await pfxFilePicker.PickSingleFileAsync();
            if (pfxFile != null)
            {
                IBuffer buffer = await FileIO.ReadBufferAsync(pfxFile);
                using (DataReader dataReader = DataReader.FromBuffer(buffer))
                {
                    byte[] bytes = new byte[buffer.Length];
                    dataReader.ReadBytes(bytes);
                    pfxCert = System.Convert.ToBase64String(bytes);
                    PfxPassword.Password = string.Empty;
                    result.Append("succeeded");
                }
            }
            else
            {
                result.Append("failed");
            }
        }
        catch (Exception ex)
        {
            result.Append("failed with ");
            result.Append(ex.Message); ;
        }
    
        Result.Text = result.ToString();
    }
    
  8. 執行您的應用程式並登入您的安全 Web 服務,並將 PFX 檔案匯入本機憑證存放區。

您可以使用這些步驟建立多個應用程式,這些應用程式會使用相同的使用者憑證來存取相同或不同的安全 Web 服務。