Add PDF signing feature with UI and backend support
Introduced a new `PdfSigningService` for digitally signing PDFs using a PFX certificate and DevExpress's `PdfDocumentSigner`. Updated `Program.cs` to register the service and added a new configuration section `PdfSigning` in `appsettings.json` for certificate management. Added a Razor Page `TestSignature.cshtml` and its page model `TestSignature.cshtml.cs` to provide a user interface for testing PDF signing. The page includes file upload, signature validation, and result display functionality. Implemented supporting classes `PdfSigningOptions` for signature customization and `SignatureInformation` for extracting and displaying signature details, including signer name, location, and certificate validity.
This commit is contained in:
82
DXApp.TemplateKitProject/Pages/TestSignature.cshtml.cs
Normal file
82
DXApp.TemplateKitProject/Pages/TestSignature.cshtml.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using DXApp.TemplateKitProject.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
||||
namespace DXApp.TemplateKitProject.Pages;
|
||||
|
||||
public class TestSignatureModel(
|
||||
PdfSigningService signingService,
|
||||
IConfiguration configuration,
|
||||
ILogger<TestSignatureModel> logger) : PageModel
|
||||
{
|
||||
[BindProperty]
|
||||
public IFormFile? PdfFile { get; set; }
|
||||
|
||||
public bool Success { get; private set; }
|
||||
public string? ErrorMessage { get; private set; }
|
||||
public string? OriginalFileName { get; private set; }
|
||||
public long OriginalSizeKb { get; private set; }
|
||||
public string? SignedFileName { get; private set; }
|
||||
public long SignedSizeKb { get; private set; }
|
||||
public string? OutputPath { get; private set; }
|
||||
public List<SignatureInformation>? SignatureInfo { get; private set; }
|
||||
|
||||
public void OnGet()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
if (PdfFile is null || PdfFile.Length == 0)
|
||||
{
|
||||
ModelState.AddModelError(nameof(PdfFile), "Bitte eine PDF-Datei auswählen.");
|
||||
return Page();
|
||||
}
|
||||
|
||||
if (!PdfFile.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
ModelState.AddModelError(nameof(PdfFile), "Nur PDF-Dateien sind erlaubt.");
|
||||
return Page();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
OriginalFileName = PdfFile.FileName;
|
||||
|
||||
// PDF in Byte-Array laden
|
||||
using var memStream = new MemoryStream();
|
||||
await PdfFile.CopyToAsync(memStream);
|
||||
var originalBytes = memStream.ToArray();
|
||||
OriginalSizeKb = originalBytes.Length / 1024;
|
||||
|
||||
logger.LogInformation("Signiere PDF: {FileName} ({Size} KB)", OriginalFileName, OriginalSizeKb);
|
||||
|
||||
// PDF signieren
|
||||
var signedBytes = await signingService.SignPdfAsync(originalBytes);
|
||||
SignedSizeKb = signedBytes.Length / 1024;
|
||||
|
||||
// Speichern
|
||||
var outputDir = configuration["PdfResults:OutputDirectory"] ?? Path.GetTempPath();
|
||||
Directory.CreateDirectory(outputDir);
|
||||
|
||||
SignedFileName = Path.GetFileNameWithoutExtension(PdfFile.FileName) + "_signed.pdf";
|
||||
OutputPath = Path.Combine(outputDir, SignedFileName);
|
||||
|
||||
await System.IO.File.WriteAllBytesAsync(OutputPath, signedBytes);
|
||||
|
||||
logger.LogInformation("Signierte PDF gespeichert: {Path}", OutputPath);
|
||||
|
||||
// Signatur-Informationen auslesen
|
||||
SignatureInfo = await signingService.GetSignatureInfoAsync(signedBytes);
|
||||
|
||||
Success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Fehler beim Signieren der PDF: {FileName}", PdfFile.FileName);
|
||||
ErrorMessage = ex.Message;
|
||||
}
|
||||
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user