Verify Digital Signatures in Documents using C#
Tags: dotnet, csharp, webdev, digitalsignature
Published: 2019-09-26 05:38:32

**Digital Signatures** in documents look similar to the paper-based signatures, however, being certificate-based **electronic signatures** they contain the identity of the signer in encrypted form. The certificates are issued by trusted and authorized Certificate Authorities and they identify the person they are issued to. This is why the digitally signed documents can be verified at any time. In this article, I’ll show you how to programmatically verify the digital signature in **PDF**, **Word**, and **Excel**, **documents **by using [**GroupDocs.Signature for .NET**](https://products.groupdocs.com/signature/net)** API **with **C#**.

### Steps to verify digitally signed PDF document

For demonstration, I am using a PDF document for digital signature verification. However, the same code will work for MS Word and Excel document formats.

**1.** Download _[GroupDocs.Signature for .NET](https://downloads.groupdocs.com/signature/net)_ or install it using [NuGet](https://www.nuget.org/packages/GroupDocs.Signature/).

**2.** Add the following namespaces in your code.

```csharp
using GroupDocs.Signature;
using GroupDocs.Signature.Domain;
using GroupDocs.Signature.Options;
```

**3.** Load digitally signed PDF document using an instance of [_Signature_](https://apireference.groupdocs.com/net/signature/groupdocs.signature/signature) class.

```csharp
using (Signature signature = new Signature("sample.pdf"))
{
    // Your code goes here.
}
```

**4.** Instantiate the _[DigitalVerifyOptions](https://apireference.groupdocs.com/net/signature/groupdocs.signature.options/digitalverifyoptions)_ object and specify verification options.

```csharp
DigitalVerifyOptions options = new DigitalVerifyOptions("certificate.pfx")
{
    Comments = "Test comment"
};
```

**5.** Call _[Verify](https://apireference.groupdocs.com/net/signature/groupdocs.signature/signature/methods/verify)_ method of _Signature _class’ instance and pass _DigitalVerifyOptions _to it.
```csharp
// verify document signatures
VerificationResult result = signature.Verify(options);
```
**6.** Check verification results from _[VerificationResult](https://apireference.groupdocs.com/net/signature/groupdocs.signature.domain/verificationresult)_ object.

```csharp
if (result.IsValid)
{
    Console.WriteLine("\nDocument was verified successfully!");
}
else
{
    Console.WriteLine("\nDocument failed verification process.");
}
```

### Complete Code

```csharp
using GroupDocs.Signature;
using GroupDocs.Signature.Domain;
using GroupDocs.Signature.Options;

using (Signature signature = new Signature("sample.pdf"))
{
    DigitalVerifyOptions options = new DigitalVerifyOptions("certificate.pfx")
    {
        Comments = "Test comment"
    };
    // verify document signatures
    VerificationResult result = signature.Verify(options);
    if (result.IsValid)
    {
        Console.WriteLine("\nDocument was verified successfully!");
    }
    else
    {
        Console.WriteLine("\nDocument failed verification process.");
    }
}
```

Thus, you can determine whether the digital signature in the PDF document meets the specified criteria or not. Finally, you can mark the document as valid or invalid. 

Cheers!