共用方式為


XpsDocument.SignDigitally 方法

定義

使用指定的 X.509 憑證,簽署封裝組件的集合。

多載

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions)

以指定的 X.509 憑證簽署封裝組件的集合。

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions, Guid)

使用指定的 X.509 憑證,簽署封裝組件的集合。

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions, Guid, Boolean)

以指定的 X.509 憑證簽署封裝組件的集合。

範例

下列範例會示範如何使用 SignDigitally 方法。


// ------------------------------ SignXps -----------------------------
/// <summary>
///   Signs an XPS document with a given X509 certificate, and if one
///   exists, associates the signature with a given SignatureDefintion
///   spotId GUID.</summary>
/// <param name="xpsDocument">
///   The XPS document to sign.</param>
/// <param name="cert">
///   The X509 certificate to use for signing.</param>
/// <param name="spotId">
///   The nullable spotId GUID of the signature definition.</param>
public void SignXps(
    XpsDocument xpsDocument, X509Certificate cert, Guid? spotId )
{
    // If there's a SignatureDefinition spotId,
    // associate it with the signature.
    if (spotId != null)
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None,
            spotId.Value);
    }
    else
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None);
    }
}// end:SignXps()

' ------------------------------ SignXps -----------------------------
''' <summary>
'''   Signs an XPS document with a given X509 certificate, and if one
'''   exists, associates the signature with a given SignatureDefintion
'''   spotId GUID.</summary>
''' <param name="xpsDocument">
'''   The XPS document to sign.</param>
''' <param name="cert">
'''   The X509 certificate to use for signing.</param>
''' <param name="spotId">
'''   The nullable spotId GUID of the signature definition.</param>
Public Sub SignXps(xpsDocument As XpsDocument, cert As X509Certificate, spotId? As Guid)
    ' If there's a SignatureDefinition spotId,
    ' associate it with the signature.
    If spotId IsNot Nothing Then
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None, spotId.Value)
    Else
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None)
    End If

End Sub

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions)

以指定的 X.509 憑證簽署封裝組件的集合。

public:
 System::Windows::Xps::Packaging::XpsDigitalSignature ^ SignDigitally(System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate, bool embedCertificate, System::Windows::Xps::Packaging::XpsDigSigPartAlteringRestrictions restrictions);
public System.Windows.Xps.Packaging.XpsDigitalSignature SignDigitally (System.Security.Cryptography.X509Certificates.X509Certificate certificate, bool embedCertificate, System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions restrictions);
member this.SignDigitally : System.Security.Cryptography.X509Certificates.X509Certificate * bool * System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions -> System.Windows.Xps.Packaging.XpsDigitalSignature
Public Function SignDigitally (certificate As X509Certificate, embedCertificate As Boolean, restrictions As XpsDigSigPartAlteringRestrictions) As XpsDigitalSignature

參數

certificate
X509Certificate

要用於簽署每個組件的 X.509 憑證;若要提示使用者選取已安裝的憑證,則為 null

embedCertificate
Boolean

true 表示要將憑證儲存在封裝中;否則為 false

restrictions
XpsDigSigPartAlteringRestrictions

旗標,表示哪些相依組件必須排除在簽署之外。

傳回

XpsDigitalSignature,包含簽章的相關資訊。

範例

下列範例會示範如何使用 SignDigitally 方法。


// ------------------------------ SignXps -----------------------------
/// <summary>
///   Signs an XPS document with a given X509 certificate, and if one
///   exists, associates the signature with a given SignatureDefintion
///   spotId GUID.</summary>
/// <param name="xpsDocument">
///   The XPS document to sign.</param>
/// <param name="cert">
///   The X509 certificate to use for signing.</param>
/// <param name="spotId">
///   The nullable spotId GUID of the signature definition.</param>
public void SignXps(
    XpsDocument xpsDocument, X509Certificate cert, Guid? spotId )
{
    // If there's a SignatureDefinition spotId,
    // associate it with the signature.
    if (spotId != null)
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None,
            spotId.Value);
    }
    else
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None);
    }
}// end:SignXps()

' ------------------------------ SignXps -----------------------------
''' <summary>
'''   Signs an XPS document with a given X509 certificate, and if one
'''   exists, associates the signature with a given SignatureDefintion
'''   spotId GUID.</summary>
''' <param name="xpsDocument">
'''   The XPS document to sign.</param>
''' <param name="cert">
'''   The X509 certificate to use for signing.</param>
''' <param name="spotId">
'''   The nullable spotId GUID of the signature definition.</param>
Public Sub SignXps(xpsDocument As XpsDocument, cert As X509Certificate, spotId? As Guid)
    ' If there's a SignatureDefinition spotId,
    ' associate it with the signature.
    If spotId IsNot Nothing Then
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None, spotId.Value)
    Else
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None)
    End If

End Sub

備註

如果 為 certificatenull ,則會出現一個對話方塊,可讓使用者選取已安裝的 X.509 憑證,例如從智慧卡閱讀機。

適用於

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions, Guid)

使用指定的 X.509 憑證,簽署封裝組件的集合。

public:
 System::Windows::Xps::Packaging::XpsDigitalSignature ^ SignDigitally(System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate, bool embedCertificate, System::Windows::Xps::Packaging::XpsDigSigPartAlteringRestrictions restrictions, Guid id);
public System.Windows.Xps.Packaging.XpsDigitalSignature SignDigitally (System.Security.Cryptography.X509Certificates.X509Certificate certificate, bool embedCertificate, System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions restrictions, Guid id);
member this.SignDigitally : System.Security.Cryptography.X509Certificates.X509Certificate * bool * System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions * Guid -> System.Windows.Xps.Packaging.XpsDigitalSignature
Public Function SignDigitally (certificate As X509Certificate, embedCertificate As Boolean, restrictions As XpsDigSigPartAlteringRestrictions, id As Guid) As XpsDigitalSignature

參數

certificate
X509Certificate

要用於簽署每個組件的 X.509 憑證;若要提示使用者選取已安裝的憑證,則為 null

embedCertificate
Boolean

true 表示要將憑證儲存在封裝中;否則為 false

restrictions
XpsDigSigPartAlteringRestrictions

旗標,表示哪些相依組件要排除在簽署之外。

id
Guid

指派給簽章的識別碼。

傳回

XpsDigitalSignature,包含簽章的相關資訊。

範例

下列範例會示範如何使用 SignDigitally 方法。


// ------------------------------ SignXps -----------------------------
/// <summary>
///   Signs an XPS document with a given X509 certificate, and if one
///   exists, associates the signature with a given SignatureDefintion
///   spotId GUID.</summary>
/// <param name="xpsDocument">
///   The XPS document to sign.</param>
/// <param name="cert">
///   The X509 certificate to use for signing.</param>
/// <param name="spotId">
///   The nullable spotId GUID of the signature definition.</param>
public void SignXps(
    XpsDocument xpsDocument, X509Certificate cert, Guid? spotId )
{
    // If there's a SignatureDefinition spotId,
    // associate it with the signature.
    if (spotId != null)
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None,
            spotId.Value);
    }
    else
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None);
    }
}// end:SignXps()

' ------------------------------ SignXps -----------------------------
''' <summary>
'''   Signs an XPS document with a given X509 certificate, and if one
'''   exists, associates the signature with a given SignatureDefintion
'''   spotId GUID.</summary>
''' <param name="xpsDocument">
'''   The XPS document to sign.</param>
''' <param name="cert">
'''   The X509 certificate to use for signing.</param>
''' <param name="spotId">
'''   The nullable spotId GUID of the signature definition.</param>
Public Sub SignXps(xpsDocument As XpsDocument, cert As X509Certificate, spotId? As Guid)
    ' If there's a SignatureDefinition spotId,
    ' associate it with the signature.
    If spotId IsNot Nothing Then
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None, spotId.Value)
    Else
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None)
    End If

End Sub

備註

如果 為 certificatenull ,則會出現一個對話方塊,可讓使用者選取已安裝的 X.509 憑證,例如從智慧卡閱讀機。

適用於

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions, Guid, Boolean)

以指定的 X.509 憑證簽署封裝組件的集合。

public:
 System::Windows::Xps::Packaging::XpsDigitalSignature ^ SignDigitally(System::Security::Cryptography::X509Certificates::X509Certificate ^ certificate, bool embedCertificate, System::Windows::Xps::Packaging::XpsDigSigPartAlteringRestrictions restrictions, Guid id, bool testIsSignable);
public System.Windows.Xps.Packaging.XpsDigitalSignature SignDigitally (System.Security.Cryptography.X509Certificates.X509Certificate certificate, bool embedCertificate, System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions restrictions, Guid id, bool testIsSignable);
member this.SignDigitally : System.Security.Cryptography.X509Certificates.X509Certificate * bool * System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions * Guid * bool -> System.Windows.Xps.Packaging.XpsDigitalSignature
Public Function SignDigitally (certificate As X509Certificate, embedCertificate As Boolean, restrictions As XpsDigSigPartAlteringRestrictions, id As Guid, testIsSignable As Boolean) As XpsDigitalSignature

參數

certificate
X509Certificate

要用於簽署每個組件的 X.509 憑證;若要提示使用者選取已安裝的憑證,則為 null

embedCertificate
Boolean

true 表示要將憑證儲存在封裝中;否則為 false

restrictions
XpsDigSigPartAlteringRestrictions

旗標,表示哪些相依組件要排除在簽署之外。

id
Guid

指派給簽章的識別碼。

testIsSignable
Boolean

true 表示要在簽署前確認 IsSignabletrue;否則為 false

傳回

XpsDigitalSignature,包含簽章的相關資訊。

範例

下列範例會示範如何使用 SignDigitally 方法。


// ------------------------------ SignXps -----------------------------
/// <summary>
///   Signs an XPS document with a given X509 certificate, and if one
///   exists, associates the signature with a given SignatureDefintion
///   spotId GUID.</summary>
/// <param name="xpsDocument">
///   The XPS document to sign.</param>
/// <param name="cert">
///   The X509 certificate to use for signing.</param>
/// <param name="spotId">
///   The nullable spotId GUID of the signature definition.</param>
public void SignXps(
    XpsDocument xpsDocument, X509Certificate cert, Guid? spotId )
{
    // If there's a SignatureDefinition spotId,
    // associate it with the signature.
    if (spotId != null)
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None,
            spotId.Value);
    }
    else
    {
        xpsDocument.SignDigitally(
            cert, true, XpsDigSigPartAlteringRestrictions.None);
    }
}// end:SignXps()

' ------------------------------ SignXps -----------------------------
''' <summary>
'''   Signs an XPS document with a given X509 certificate, and if one
'''   exists, associates the signature with a given SignatureDefintion
'''   spotId GUID.</summary>
''' <param name="xpsDocument">
'''   The XPS document to sign.</param>
''' <param name="cert">
'''   The X509 certificate to use for signing.</param>
''' <param name="spotId">
'''   The nullable spotId GUID of the signature definition.</param>
Public Sub SignXps(xpsDocument As XpsDocument, cert As X509Certificate, spotId? As Guid)
    ' If there's a SignatureDefinition spotId,
    ' associate it with the signature.
    If spotId IsNot Nothing Then
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None, spotId.Value)
    Else
        xpsDocument.SignDigitally(cert, True, XpsDigSigPartAlteringRestrictions.None)
    End If

End Sub

備註

如果 為 certificatenull ,則會顯示一個對話方塊,讓使用者能夠選取已安裝的 X.509 憑證,例如從智慧卡讀取器。

CryptographicException 如果 certificatenull 且未安裝任何憑證,則會擲回 。

適用於