private static async Task Run(AppConfiguration configuration) { var validator = new ConfigurationValidator(configuration); if (!validator.Validate()) { return; } using var httpClient = new HttpClient(); using var tokenProviderFactory = new LogicTokenProviderFactory(configuration.TokenProvider); var fileSecurityClient = new FileSecurityClient(httpClient, tokenProviderFactory, configuration.FileSecurityOptions); // Get Sign Configuration Log.Information("Getting signconfiguration..."); var signConfigurationResult = await fileSecurityClient.GetPdfSignConfiguration(configuration.SignConfigurationDetails.SignConfigurationId).ConfigureAwait(false); if (signConfigurationResult == null) { Log.Error("Couldn't get signconfiguration"); return; } Console.WriteLine( "Configuration recieved successfully. SignConfiguration ID: {0} \nSignConfiguration Name: {1}\nSubscription ID : {2}", signConfigurationResult.Id, signConfigurationResult.Name, signConfigurationResult.SubscriptionId); // Generate document Log.Information("Generate document using privileges..."); string pdfSample = configuration.SignConfigurationDetails.PdfEmptySampleLocation; using Document document = new Document(pdfSample); var documentPrivilege = FillPrivileges(signConfigurationResult.PdfPrivilege); document.Encrypt(string.Empty, "owner", documentPrivilege, CryptoAlgorithm.AESx128, false); document.Save(configuration.SignConfigurationDetails.PdfGeneratedDocumentLocation + "pdf_with_privileges.pdf"); Log.Information( "Document with configured privileges generated successfully at {location}", configuration.SignConfigurationDetails.PdfGeneratedDocumentLocation + "pdf-with-privileges.pdf"); }
private static async Task Run(AppConfiguration configuration) { var validator = new ConfigurationValidator(configuration); if (!validator.Validate()) { return; } using var httpClient = new HttpClient(); using var tokenProviderFactory = new LogicTokenProviderFactory(configuration.TokenProvider); var fileSecurityClient = new FileSecurityClient(httpClient, tokenProviderFactory, configuration.FileSecurityOptions); var certificateId = configuration.CertificateDetails.CertificateId; Log.Information("Fetching certificate details for certificate id {CertificateId} ", configuration.CertificateDetails.CertificateId); var result = await fileSecurityClient.GetCertificate(certificateId).ConfigureAwait(false); if (result == null) { Log.Error("Invalid certificate id {Id}", configuration.CertificateDetails.CertificateId); return; } Console.WriteLine("Certificate ID: {0} \nCertificate Name: {1}\nSubscription ID : {2}", result.CertificateId, result.Name, result.SubscriptionId); // Create a Sign Configuration var signConfigurationRequest = BuildSignConfigurationRequest(configuration); Log.Information("Creating signconfiguration..."); var signConfigurationResult = await fileSecurityClient.CreateSignConfigurationPdf(signConfigurationRequest).ConfigureAwait(false); if (signConfigurationResult == null) { Log.Error("Couldn't create signconfiguration"); return; } Console.WriteLine( "Configuration created successfully. SignConfiguration ID: {0} \nSignConfiguration Name: {1}\nSubscription ID : {2}", signConfigurationResult.Id, signConfigurationResult.Name, signConfigurationResult.SubscriptionId); // Get a Sign Configuration var signConfigurationID = configuration.SignConfigurationDetails.SignConfigurationId; Log.Information("Fetching sign configuration details for sign configuration id {SignConfigurationId}", configuration.SignConfigurationDetails.SignConfigurationId); var signConfigurationGetResult = await fileSecurityClient.GetPdfSignConfiguration(signConfigurationID); if (signConfigurationGetResult == null) { Log.Error("Sign configuration not found for id {Id}", configuration.SignConfigurationDetails.SignConfigurationId); return; } Console.WriteLine( "Sign Configuration Details. \nSignConfiguration ID: {0} " + "\nSignConfiguration Name: {1}" + "\nCertificate ID : {2}" + "\nSubscription ID : {3}" + "\nCreatedDate : {4}" + "\nDocumentType : {5}" + "\nPrivileges : {6}", signConfigurationGetResult.Id, signConfigurationGetResult.Name, signConfigurationGetResult.CertificateId, signConfigurationGetResult.SubscriptionId, signConfigurationGetResult.CreatedDate, "Pdf", Newtonsoft.Json.JsonConvert.SerializeObject(signConfigurationGetResult.PdfPrivilege, Newtonsoft.Json.Formatting.Indented)); }