Example #1
0
        public static async Task CreateAndSend()
        {
            ClientConfiguration clientConfiguration = null; //As initialized earlier
            var portalClient = new PortalClient(clientConfiguration);

            var documentToSign = new Document(
                "Subject of Message",
                "This is the content",
                FileType.Pdf,
                @"C:\Path\ToDocument\File.pdf"
                );

            var signers = new List <Signer>
            {
                new Signer(new PersonalIdentificationNumber("00000000000"), new NotificationsUsingLookup()),
                new Signer(new PersonalIdentificationNumber("11111111111"), new Notifications(
                               new Email("*****@*****.**"),
                               new Sms("999999999"))),
                new Signer(new ContactInformation {
                    Email = new Email("*****@*****.**")
                }),
                new Signer(new ContactInformation {
                    Sms = new Sms("88888888")
                }),
                new Signer(new ContactInformation
                {
                    Email = new Email("*****@*****.**"),
                    Sms   = new Sms("77777777")
                })
            };

            var portalJob = new Job(documentToSign, signers, "myReferenceToJob");

            var portalJobResponse = await portalClient.Create(portalJob);
        }
Example #2
0
        public static async Task SpecifyingQueues()
        {
            PortalClient portalClient = null; //As initialized earlier

            var organizationNumber = "123456789";
            var sender             = new Sender(organizationNumber, new PollingQueue("CustomPollingQueue"));

            var documentToSign = new Document(
                "Subject of Message",
                "This is the content",
                FileType.Pdf,
                @"C:\Path\ToDocument\File.pdf"
                );

            var signers = new List <Signer>
            {
                new Signer(new PersonalIdentificationNumber("00000000000"), new NotificationsUsingLookup())
            };

            var portalJob = new Job(documentToSign, signers, "myReferenceToJob", sender);

            var portalJobResponse = await portalClient.Create(portalJob);

            var changedJob = await portalClient.GetStatusChange(sender);
        }
Example #3
0
        public static async void Cancel()
        {
            PortalClient portalClient = null; //As initialized earlier
            Job          portalJob    = null; //As initialized earlier

            var portalJobResponse = await portalClient.Create(portalJob);

            await portalClient.Cancel(portalJobResponse.CancellationReference);
        }
Example #4
0
        private TestHelper Create(Job job, Sender optionalSender = null)
        {
            if (optionalSender != null)
            {
                job.Sender = optionalSender;
            }

            _job                   = job;
            _jobResponse           = _client.Create(_job).Result;
            _cancellationReference = new CancellationReference(TransformReferenceToCorrectEnvironment(_jobResponse.CancellationReference.Url));

            return(this);
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Starting dat loggah ...");
            Log.Debug("Debug logging");
            Log.Info("Info logging");
            Log.Warn("Warn logging");
            Log.Error("Error logging");
            Log.Fatal("Fatal logging");

            var stringPrivateOrganizationNumber = "088015814";
            var stringPublicOrganizationNumber  = "988015814";

            var testEnvironment = Environment.DifiTest;

            testEnvironment.Url = new Uri("https://api.test.signering.posten.no");

            var client = new PortalClient(
                new ClientConfiguration(
                    testEnvironment,
                    "2d 7f 30 dd 05 d3 b7 fc 7a e5 97 3a 73 f8 49 08 3b 20 40 ed",
                    new Sender(stringPublicOrganizationNumber)
                    )
            {
                LogRequestAndResponse = true
            }
                );

            var signer = new Signer(new PersonalIdentificationNumber("01043100358"), new Notifications(new Email("*****@*****.**")))
            {
                OnBehalfOf = OnBehalfOf.Other
            };

            var created = client.Create(new Job(
                                            new Document("Et signeringsoppdrag", "Her kommer en melding", FileType.Pdf, @"\\vmware-host\Shared Folders\Downloads\00370726201232902222.pdf"),
                                            new List <Signer> {
                signer
            },
                                            "Avsenders referance", new Sender(stringPrivateOrganizationNumber))).Result;

            var status = client.GetStatusChange(new Sender(stringPrivateOrganizationNumber)).Result;

            Console.WriteLine("Finished with loggah ...");
            Console.ReadLine();
        }
            public async Task Throws_exception_on_no_sender()
            {
                //Arrange
                var clientConfiguration = new ClientConfiguration(Environment.DifiQa, GetPostenTestCertificate());
                var portalClient        = new PortalClient(clientConfiguration);
                var portalJob           = new Job(DomainUtility.GetPortalDocument(), DomainUtility.GetSigners(1), "SendersReference");

                //Act
                await Assert.ThrowsAsync <SenderNotSpecifiedException>(async() => await portalClient.Create(portalJob).ConfigureAwait(false)).ConfigureAwait(false);
            }
Example #7
0
        //The main function which is performed each time a document is sent to the workflow
        void ITheWorkflowSyncEvtEx.ProcAutomaticInst(int nInstanceNo, int nTokenNo, string bstrParams)
        {
            try
            {
                TheServer server = new TheServer();
                server.Connect(TheClientType.CustomApplication);

                //create a TheWFInstance object and load the workflow item by using the instance number
                TheWFInstance wfInstance = new TheWFInstance();
                wfInstance.Load(server, nInstanceNo);

                //the GetLinkedDocAt method gives back the document number
                //of the document this workflow item belongs to
                TheWFDocument wfDoc = wfInstance.GetLinkedDocAt(0);

                int nDocNo = wfInstance.GetLinkedDocAt(0).DocNo;

                //with the document number you can retrieve the document
                TheDocument theDoc    = new TheDocument();
                string      strTheDoc = theDoc.Retrieve(wfDoc.DocNo, "", server);

                //A temporary location for storing files which are extracted from Therefore (and will be sent for signing).

                string extractFile = theDoc.ExtractStream(0, Path.GetTempPath());

                string sOrganizationNo;
                string sCertThumbprint;
                string sRecieverEmail, vRecieverEmail = null;
                string sRecieverNIN, vRecieverNIN = null;
                string sRecieverPhone, vRecieverPhone = null;
                string sSubject, vSubject;
                string sTitle, vTitle;
                string vReference;
                string sStatus;
                string sMStatus = "";
                string sJobId;
                string sTableFieldNo = "";
                string sNINInSignature;
                string sEnvironment;
                string sDebugMode;
                int    iDebugMode           = 0;
                int    sDefaultAvailability = 4;
                string sFAvailability       = "";

                //Load information about which fields to use in the category from the Registry
                RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
                key = key.OpenSubKey("SOFTWARE").OpenSubKey("Canon");


                sOrganizationNo = key.GetValue("OrganizationNo").ToString();
                sCertThumbprint = key.GetValue("CertThumbprint").ToString();
                sEnvironment    = key.GetValue("Environment").ToString();
                sDebugMode      = key.GetValue("DebugMode").ToString();
                if (sDebugMode != "")
                {
                    iDebugMode = Convert.ToInt32(sDebugMode);
                }
                key            = key.OpenSubKey(wfInstance.ProcessName);
                sRecieverEmail = key.GetValue("RecieverEmailFieldName").ToString();
                sRecieverNIN   = key.GetValue("RecieverNINFieldName").ToString();

                sRecieverPhone = key.GetValue("RecieverPhoneFieldName").ToString();
                sSubject       = key.GetValue("SubjectFieldName").ToString();
                sTitle         = key.GetValue("TitleFieldName").ToString();

                sStatus              = key.GetValue("StatusFieldName").ToString();
                sJobId               = key.GetValue("JobIDFieldName").ToString();
                sNINInSignature      = key.GetValue("NINInSignature").ToString();
                sDefaultAvailability = Convert.ToInt32(key.GetValue("DefaultAvailablity").ToString());
                sFAvailability       = key.GetValue("AvailabilityFieldName").ToString();

                //If there are multiple signers, we need to use a table for storing the data
                sTableFieldNo = key.GetValue("M_TableFieldNo").ToString();
                if (sTableFieldNo != "")
                {
                    sRecieverEmail = key.GetValue("M_MailFieldName").ToString();
                    sMStatus       = key.GetValue("M_StatusFieldName").ToString();
                    sRecieverPhone = key.GetValue("M_PhoneFieldName").ToString();
                    sRecieverNIN   = key.GetValue("M_NINFieldName").ToString();
                }

                //Creating a list which contains all Signers (POSTEN)
                List <Signer> listOfSigners = new List <Signer>();

                //loading the indexdata related to the document
                TheIndexData     indata   = new TheIndexData();
                TheTableDataType inTddata = new TheTableDataType();
                indata = theDoc.IndexData;

                //If only one signer:
                if (sTableFieldNo == "")
                {
                    if (!String.IsNullOrEmpty(sRecieverEmail))
                    {
                        vRecieverEmail = indata.GetValueByColName(sRecieverEmail);
                    }
                    if (!String.IsNullOrEmpty(sRecieverPhone))
                    {
                        vRecieverPhone = indata.GetValueByColName(sRecieverPhone);
                    }
                    if (!String.IsNullOrEmpty(sRecieverNIN))
                    {
                        vRecieverNIN = indata.GetValueByColName(sRecieverNIN);
                    }
                    if (!string.IsNullOrEmpty(vRecieverNIN) && !string.IsNullOrEmpty(vRecieverEmail) && !string.IsNullOrEmpty(vRecieverPhone))
                    {
                        listOfSigners.Add(new Signer(new PersonalIdentificationNumber(vRecieverNIN), new Notifications(
                                                         new Email(vRecieverEmail),
                                                         new Sms(vRecieverPhone))));
                    }
                    else if (!string.IsNullOrEmpty(vRecieverEmail) && !string.IsNullOrEmpty(vRecieverPhone))
                    {
                        listOfSigners.Add(new Signer(new ContactInformation {
                            Email = new Email(vRecieverEmail), Sms = new Sms(vRecieverPhone)
                        }));
                    }
                    else if (!string.IsNullOrEmpty(vRecieverEmail))
                    {
                        listOfSigners.Add(new Signer(new ContactInformation {
                            Email = new Email(vRecieverEmail)
                        }));
                    }
                    if (iDebugMode > 0)
                    {
                        foreach (Signer logSigner in listOfSigners)
                        {
                            Library.WriteErrorLog("Signatør lagt til: " + logSigner.Identifier.ToString() + System.Environment.NewLine);
                        }
                    }
                }
                //IF multiple signers:
                else
                {
                    inTddata = indata.GetTableValue(Convert.ToInt32(sTableFieldNo));
                    for (int l = 0; l < inTddata.RowCount; l++)
                    {
                        vRecieverEmail = inTddata.GetValue(sRecieverEmail, l);
                        vRecieverPhone = inTddata.GetValue(sRecieverPhone, l).ToString();
                        vRecieverNIN   = inTddata.GetValue(sRecieverNIN, l);
                        if (!string.IsNullOrEmpty(vRecieverNIN) && !string.IsNullOrEmpty(vRecieverEmail) && !string.IsNullOrEmpty(vRecieverPhone))
                        {
                            listOfSigners.Add(new Signer(new PersonalIdentificationNumber(vRecieverNIN), new Notifications(
                                                             new Email(vRecieverEmail),
                                                             new Sms(vRecieverPhone))));
                        }
                        else if (!string.IsNullOrEmpty(vRecieverEmail) && !string.IsNullOrEmpty(vRecieverPhone))
                        {
                            listOfSigners.Add(new Signer(new ContactInformation {
                                Email = new Email(vRecieverEmail), Sms = new Sms(vRecieverPhone)
                            }));
                        }
                        else if (!string.IsNullOrEmpty(vRecieverEmail))
                        {
                            listOfSigners.Add(new Signer(new ContactInformation {
                                Email = new Email(vRecieverEmail)
                            }));
                        }
                        if (l == 9)
                        {
                            break;
                        }
                    }
                }

                //Get information from the category which is needed to create a signing job

                vReference = indata.DocNo.ToString();
                vTitle     = indata.GetValueByColName(sTitle);
                vSubject   = indata.GetValueByColName(sSubject);


                //Get the certificate from the sigature store on the locale computer
                X509Store        storeMy = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                X509Certificate2 tekniskAvsenderSertifikat;
                try
                {
                    storeMy.Open(OpenFlags.ReadOnly);
                    var certs = storeMy.Certificates;

                    tekniskAvsenderSertifikat = certs.Find(
                        X509FindType.FindByThumbprint, CleanThumbprint(sCertThumbprint), false)[0];
                    if (iDebugMode > 0)
                    {
                        Library.WriteErrorLog("Certificate found: " + tekniskAvsenderSertifikat.ToString() + System.Environment.NewLine);
                    }
                }
                finally
                {
                    storeMy.Close();
                }
                //POSTEN: Portalclient
                PortalClient portalClient = null;

                Digipost.Signature.Api.Client.Core.Environment env = Digipost.Signature.Api.Client.Core.Environment.DifiTest;
                if (sEnvironment == "Production")
                {
                    env = Digipost.Signature.Api.Client.Core.Environment.Production;
                }

                //POSTEN: Create Client Configuration
                ClientConfiguration clientconfiguration = new ClientConfiguration(
                    env,
                    CleanThumbprint(sCertThumbprint),
                    new Sender(sOrganizationNo.Trim()));
                if (iDebugMode > 0)
                {
                    Library.WriteErrorLog("Client configured : " + clientconfiguration.ToString() + System.Environment.NewLine);
                }

                portalClient = new PortalClient(clientconfiguration);
                if (iDebugMode > 0)
                {
                    Library.WriteErrorLog("Portal configured : " + portalClient.ToString() + System.Environment.NewLine);
                }

                //POSTEN: Document
                Document documentToSign = new Document(
                    vTitle,
                    vSubject,
                    FileType.Pdf,
                    extractFile);
                if (iDebugMode > 0)
                {
                    Library.WriteErrorLog("Document configured : " + documentToSign.ToString() + System.Environment.NewLine);
                    Library.WriteErrorLog("PDF Path : " + extractFile + System.Environment.NewLine);
                }
                //POSTEN: Job
                Job job = new Job(
                    documentToSign,
                    listOfSigners,
                    vReference
                    );
                TimeSpan aTS = new TimeSpan(4 * 7, 0, 0, 0, 0);
                Library.WriteErrorLog("SF Availability : " + sFAvailability.ToString() + System.Environment.NewLine);
                if (!String.IsNullOrEmpty(sFAvailability))
                {
                    string aValue = "";
                    if (sFAvailability.Contains("|"))
                    {
                        Library.WriteErrorLog("SF Availability contains  pipe" + System.Environment.NewLine);
                        char[] splits = { '|' };
                        aValue = indata.GetValueByFieldNo(Convert.ToInt32(sFAvailability.Split(splits)[0].Trim()));
                        Library.WriteErrorLog("SF Availability is " + aValue + System.Environment.NewLine);
                    }
                    else
                    {
                        aValue = indata.GetValueByColName(sFAvailability);
                    }
                    if (!String.IsNullOrEmpty(aValue))
                    {
                        char[] cH       = { ' ' };
                        string aMeaning = aValue.Split(cH)[1];
                        int    iValue   = Convert.ToInt32(aValue.Split(cH)[0]);
                        if (aMeaning == "timer")
                        {
                            aTS = new TimeSpan(0, iValue, 0, 0, 0);
                        }
                        else if (aMeaning == "hour")
                        {
                            aTS = new TimeSpan(0, iValue, 0, 0, 0);
                        }
                        else if (aMeaning == "time")
                        {
                            aTS = new TimeSpan(0, iValue, 0, 0, 0);
                        }
                        else if (aMeaning == "hours")
                        {
                            aTS = new TimeSpan(0, iValue, 0, 0, 0);
                        }
                        else if (aMeaning == "uker")
                        {
                            aTS = new TimeSpan(iValue * 7, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "uke")
                        {
                            aTS = new TimeSpan(iValue * 7, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "week")
                        {
                            aTS = new TimeSpan(iValue * 7, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "weeks")
                        {
                            aTS = new TimeSpan(iValue * 7, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "dager")
                        {
                            aTS = new TimeSpan(iValue, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "dag")
                        {
                            aTS = new TimeSpan(iValue, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "day")
                        {
                            aTS = new TimeSpan(iValue, 0, 0, 0, 0);
                        }
                        else if (aMeaning == "days")
                        {
                            aTS = new TimeSpan(iValue, 0, 0, 0, 0);
                        }
                    }
                }
                else
                {
                    aTS = new TimeSpan(sDefaultAvailability * 7, 0, 0, 0, 0);
                }
                job.Availability = new Availability();

                job.Availability.AvailableFor = aTS;


                if (iDebugMode > 0)
                {
                    Library.WriteErrorLog("List of Signers : " + listOfSigners.ToString() + System.Environment.NewLine);

                    Library.WriteErrorLog("job configured : " + job.ToString() + System.Environment.NewLine);
                }

                //POSTEN: A job Response which acutally creates the signing job.


                JobResponse x = portalClient.Create(job).Result;
                if (iDebugMode > 0)
                {
                    Library.WriteErrorLog("JobResponse created : " + x.ToString() + System.Environment.NewLine);
                }


                //If multiple signers:
                if (sTableFieldNo != "")
                {
                    for (int l = 0; l < inTddata.RowCount; l++)
                    {
                        inTddata.SetValue(sMStatus, l, "Sendt for signering: " + x.ToString());
                        if (l == 9)
                        {
                            break;
                        }
                    }
                    indata.SetTableValue(Convert.ToInt32(sTableFieldNo), inTddata);
                    indata.SetValueByColName(sJobId, x.JobId.ToString());
                }
                //Update the status of the signing job in Therefore
                indata.SetValueByColName(sStatus, "Sendt for signering: " + x.ToString());
                indata.SetValueByColName(sJobId, x.JobId.ToString());
                indata.SaveChanges(server);

                if (System.IO.File.Exists(extractFile))
                {
                    try
                    {
                        System.IO.File.Delete(extractFile);
                    }
                    catch (Exception ex)
                    {
                        Library.WriteErrorLog("Could not delete file: " + ex.Message);
                    }
                }


                //get the next task in the workflow and finish the current task
                TheWFTask wfTask = wfInstance.GetNextTaskAt(0);
                wfInstance.ClaimInstance(server);
                wfInstance.FinishCurrentTask(server, wfTask.TaskNo, "");
            }
            catch (Exception ex)
            {
                //IF something goes wrong - write it to a log file.
                //Console.WriteLine(ex.Message);
                if (ex.InnerException != null)
                {
                    Library.WriteErrorLog("Base: " + ex.GetBaseException().Message + "|| Inner Base: " + ex.GetBaseException().InnerException.Message + " || Message: " + ex.Message + "  || INNER: " + ex.InnerException.Message + System.Environment.NewLine);
                }
                else
                {
                    Library.WriteErrorLog("Base: " + ex.GetBaseException().Message + "|| Inner Base: " + ex.GetBaseException().InnerException.Message + " || Message: " + ex.Message + System.Environment.NewLine);
                }
            }
        }