XpsDocument.SignDigitally Method

Definition

Signs a collection of package parts by using a specified X.509 certificate.

Overloads

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions)

Signs a collection of package parts with a specified X.509 certificate.

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions, Guid)

Signs a collection of package parts by using a specified X.509 certificate.

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

Signs a collection of package parts with a specified X.509 certificate.

Examples

The following example shows how to use the SignDigitally method.

C#

// ------------------------------ 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()

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions)

Signs a collection of package parts with a specified X.509 certificate.

C#
public System.Windows.Xps.Packaging.XpsDigitalSignature SignDigitally(System.Security.Cryptography.X509Certificates.X509Certificate certificate, bool embedCertificate, System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions restrictions);

Parameters

certificate
X509Certificate

The X.509 certificate to use in signing each part; or null to prompt the user to select an installed certificate.

embedCertificate
Boolean

true to store the certificate in the package; otherwise, false.

restrictions
XpsDigSigPartAlteringRestrictions

Flags that indicate what dependent parts must be excluded from the signing.

Returns

An XpsDigitalSignature that contains information about the signature.

Examples

The following example shows how to use the SignDigitally method.

C#

// ------------------------------ 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()

Remarks

If certificate is null, a dialog box appears that enables the user to select an installed X.509 certificate, such as from a smart card reader.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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

SignDigitally(X509Certificate, Boolean, XpsDigSigPartAlteringRestrictions, Guid)

Signs a collection of package parts by using a specified X.509 certificate.

C#
public System.Windows.Xps.Packaging.XpsDigitalSignature SignDigitally(System.Security.Cryptography.X509Certificates.X509Certificate certificate, bool embedCertificate, System.Windows.Xps.Packaging.XpsDigSigPartAlteringRestrictions restrictions, Guid id);

Parameters

certificate
X509Certificate

The X.509 certificate to use in signing each part; or null to prompt the user to select an installed certificate.

embedCertificate
Boolean

true to store the certificate in the package; otherwise, false.

restrictions
XpsDigSigPartAlteringRestrictions

Flags that indicate what dependent parts are excluded from the signing.

id
Guid

The ID to assign to the signature.

Returns

An XpsDigitalSignature that contains information about the signature.

Examples

The following example shows how to use the SignDigitally method.

C#

// ------------------------------ 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()

Remarks

If certificate is null, a dialog box appears that enables the user to select an installed X.509 certificate, such as from a smart card reader.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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

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

Signs a collection of package parts with a specified X.509 certificate.

C#
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);

Parameters

certificate
X509Certificate

The X.509 certificate to use in signing each part; or null to prompt the user to select an installed certificate.

embedCertificate
Boolean

true to store the certificate in the package; otherwise, false.

restrictions
XpsDigSigPartAlteringRestrictions

Flags that indicate what dependent parts to exclude from the signing.

id
Guid

The ID to assign to the signature.

testIsSignable
Boolean

true to verify that IsSignable is true before signing; otherwise, false.

Returns

An XpsDigitalSignature that contains information about the signature.

Examples

The following example shows how to use the SignDigitally method.

C#

// ------------------------------ 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()

Remarks

If certificate is null, a dialog box is presented that enables the user to select an installed X.509 certificate, such as from a smart card reader.

CryptographicException is thrown if certificate is null and no certificates are installed.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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