CertificateEmbeddingOption 列舉

定義

指定用於簽署之 X.509 憑證的儲存位置。

C#
public enum CertificateEmbeddingOption
繼承
CertificateEmbeddingOption

欄位

名稱 Description
InCertificatePart 0

憑證內嵌於本身的 PackagePart

InSignaturePart 1

憑證內嵌於針對要加入之簽章所建立的 SignaturePart

NotEmbedded 2

憑證並未內嵌於封裝中。

範例

下列範例示範如何使用 CertificateEmbeddingOption 來設定 PackageDigitalSignatureManager.CertificateOption 屬性。

C#
private static void SignAllParts(Package package)
{
    if (package == null)
        throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager
    PackageDigitalSignatureManager dsm =
        new PackageDigitalSignatureManager(package);
    dsm.CertificateOption =
        CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign
    // (GetParts() also includes PackageRelationship parts).
    System.Collections.Generic.List<Uri> toSign =
        new System.Collections.Generic.List<Uri>();
    foreach (PackagePart packagePart in package.GetParts())
    {
        // Add all package parts to the list for signing.
        toSign.Add(packagePart.Uri);
    }

    // Add the URI for SignatureOrigin PackageRelationship part.
    // The SignatureOrigin relationship is created when Sign() is called.
    // Signing the SignatureOrigin relationship disables counter-signatures.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));

    // Also sign the SignatureOrigin part.
    toSign.Add(dsm.SignatureOrigin);

    // Add the package relationship to the signature origin to be signed.
    toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

    // Sign() will prompt the user to select a Certificate to sign with.
    try
    {
        dsm.Sign(toSign);
    }

    // If there are no certificates or the SmartCard manager is
    // not running, catch the exception and show an error message.
    catch (CryptographicException ex)
    {
        MessageBox.Show(
            "Cannot Sign\n" + ex.Message,
            "No Digital Certificates Available",
            MessageBoxButton.OK,
            MessageBoxImage.Exclamation);
    }
}// end:SignAllParts()

備註

如果憑證位於 NotEmbedded 封裝中,驗證簽章的應用程式必須提供憑證的複本,才能驗證其簽署的簽章。

InSignaturePart 會將兩個參考專案 <KeyName><KeyValue>新增為預存數位簽名字段的一部分 KeyInfo<KeyName><KeyValue> 元素不會當做簽章驗證的一部分進行處理,因此不會受到修改的安全。 應用程式不應該對這兩個元素的有效性進行任何假設。 為了避免未偵測到的修改和可能的混淆,應用程式應該使用 InCertificatePart 選項,而不是 InSignaturePart。 選項 InCertificatePart 不提供或公開 <KeyName><KeyValue>

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱