private SignRequest BuildRequest(RequestSignatureType signatureType, DocumentBaseType document, Timestamp previousTimestamp) { SignRequest sr = new SignRequest(); ClaimedIdentity identity = new ClaimedIdentity(); identity.idAplicacion = _identity.ApplicationId; TimestampSignatureType signatureTypeElement = new TimestampSignatureType() { Value = signatureType.Uri }; sr.OptionalInputs = new AnyType(); if (previousTimestamp != null) { string renewXml = "<dst:RenewTimestamp xmlns:dst=\"urn:oasis:names:tc:dss:1.0:profiles:TimeStamp:schema#\"><dst:PreviousTimestamp>{0}</dst:PreviousTimestamp></dst:RenewTimestamp>"; XmlElement previousTimestampElement = GetXmlElement(string.Format(renewXml, GetXmlElement(previousTimestamp).OuterXml)); sr.OptionalInputs.Any = new XmlElement[] { GetXmlElement(signatureTypeElement), GetXmlElement(identity), previousTimestampElement }; } else { sr.OptionalInputs.Any = new XmlElement[] { GetXmlElement(signatureTypeElement), GetXmlElement(identity) }; } sr.InputDocuments = new InputDocuments(); sr.InputDocuments.Items = new object[] { document }; return(sr); }
private VerifyRequest BuildRequest(IEnumerable <object> inputDocuments, SignatureObject signatureObject, IEnumerable <XmlElement> optionalInputs) { VerifyRequest vr = new VerifyRequest(); ClaimedIdentity identity = new ClaimedIdentity(); identity.Name = new NameIdentifierType() { Value = _identity.ApplicationId }; List <XmlElement> optionalInputsList = new List <XmlElement>(); optionalInputsList.Add(GetXmlElement(identity)); foreach (var optionalInput in optionalInputs) { optionalInputsList.Add(optionalInput); } vr.OptionalInputs = new AnyType(); vr.OptionalInputs.Any = optionalInputsList.ToArray(); if (inputDocuments != null) { vr.InputDocuments = new InputDocuments(); vr.InputDocuments.Items = inputDocuments.ToArray(); } vr.SignatureObject = signatureObject; return(vr); }
/// <summary> /// Construye la petición de verificación /// </summary> /// <param name="signature"></param> /// <param name="updatedSignatureType"></param> /// <returns></returns> private VerifyRequest BuildRequest(object signature, string updatedSignatureType = null) { VerifyRequest vr = new VerifyRequest(); ClaimedIdentity identity = new ClaimedIdentity(); identity.Name = new NameIdentifierType() { Value = _identificador }; IgnoreGracePeriod igp = new IgnoreGracePeriod(); vr.OptionalInputs = new AnyType(); if (!string.IsNullOrEmpty(updatedSignatureType)) { ReturnUpdatedSignature returnUpdated = new ReturnUpdatedSignature(); returnUpdated.Type = updatedSignatureType; vr.OptionalInputs.Any = new XmlElement[] { GetXmlElement <ClaimedIdentity>(identity), GetXmlElement <ReturnUpdatedSignature>(returnUpdated), GetXmlElement <IgnoreGracePeriod>(igp) }; } else { vr.OptionalInputs.Any = new XmlElement[] { GetXmlElement <ClaimedIdentity>(identity), GetXmlElement <IgnoreGracePeriod>(igp) }; } DocumentType doc = new DocumentType(); doc.ID = "ID_DOCUMENTO"; doc.Item = signature; vr.InputDocuments = new InputDocuments(); vr.InputDocuments.Items = new object[] { doc }; vr.SignatureObject = new SignatureObject(); vr.SignatureObject.Item = new SignaturePtr() { WhichDocument = "ID_DOCUMENTO" }; return(vr); }
static void Main(string[] args) { // Custom Values string filePath = "c:/temp/demo.pdf"; // File to sign string fileMimeType = "application/pdf"; // File MIME type string username = "******"; // CoSign account username string password = "******"; // CoSign account password string domain = ""; // CoSign account domain int sigPageNum = 1; // Create signature on the first page int sigX = 145; // Signature field X location int sigY = 125; // Signature field Y location int sigWidth = 160; // Signature field width int sigHeight = 45; // Signature field height string timeFormat = "hh:mm:ss"; // The display format of the time string dateFormat = "dd/MM/yyyy"; // The display format of the date uint appearanceMask = 11; // Elements to display on the signature field (11 = Graphical image + Signer name + Time) string signatureType = "http://arx.com/SAPIWS/DSS/1.0/signature-field-create-sign"; // The actual operation of the Sign Request function string wsdlUrl = "https://prime.cosigntrial.com:8080/sapiws/dss.asmx?WSDL"; // URL to the WSDL file try { // Read file contents byte[] fileBuffer = File.ReadAllBytes(filePath); // Set file contents + MIME type (the SOAP library automatically base64 encodes the data) DocumentType document = new DocumentType() { Item = new DocumentTypeBase64Data() { Value = fileBuffer, MimeType = fileMimeType } }; ClaimedIdentity claimedIdentity = new ClaimedIdentity() { Name = new NameIdentifierType() { Value = username, NameQualifier = domain }, SupportingInfo = new CoSignAuthDataType() { LogonPassword = password } }; // Define signature field settings SAPISigFieldSettingsType sigFieldSettings = new SAPISigFieldSettingsType() { Invisible = false, InvisibleSpecified = true, X = sigX, XSpecified = true, Y = sigY, YSpecified = true, Width = sigWidth, WidthSpecified = true, Height = sigHeight, HeightSpecified = true, Page = sigPageNum, PageSpecified = true, AppearanceMask = appearanceMask, AppearanceMaskSpecified = true, TimeFormat = new TimeDateFormatType() { TimeFormat = timeFormat, DateFormat = dateFormat, ExtTimeFormat = ExtendedTimeFormatEnum.GMT, ExtTimeFormatSpecified = true } }; // Build complete request object SignRequest signRequest = new SignRequest() { InputDocuments = new RequestBaseTypeInputDocuments() { Items = new DocumentType[] { document } }, OptionalInputs = new RequestBaseTypeOptionalInputs() { SignatureType = signatureType, ClaimedIdentity = claimedIdentity, SAPISigFieldSettings = sigFieldSettings, ReturnPDFTailOnly = true, ReturnPDFTailOnlySpecified = true } }; // Initiate service client DSS client = new DSS() { Url = wsdlUrl }; // Send the request DssSignResult response = client.DssSign(signRequest); // Check response output if ("urn:oasis:names:tc:dss:1.0:resultmajor:Success".Equals(response.Result.ResultMajor)) { // On success- append signature object to the source PDF document (the SOAP library automatically decodes the base64 encoded output) byte[] signatureObjectBuffer = ((DssSignResultSignatureObjectBase64Signature)response.SignatureObject.Item).Value; using (var fileStream = new FileStream(filePath, FileMode.Append)) { fileStream.Write(signatureObjectBuffer, 0, signatureObjectBuffer.Length); } } else { // On failure- raise exception with the result error message throw new Exception(response.Result.ResultMessage.Value); } Console.WriteLine("The document has been successfully signed!"); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } }