private async void Sign_Click(object sender, RoutedEventArgs e) { X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = store.Certificates; X509Certificate2Collection fcollection = collection.Find(X509FindType.FindByKeyUsage, X509KeyUsageFlags.NonRepudiation, true); X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Sign Certificate Select", "Select a certificate to sign with", X509SelectionFlag.SingleSelection); DsspClient dsspClient = new DsspClient("https://www.e-contract.be/dss-ws/dss"); dsspClient.Application.UT.Name = Properties.Settings.Default.user; dsspClient.Application.UT.Password = Properties.Settings.Default.pwd; dsspClient.Signer = scollection.Cast <X509Certificate2>().AsQueryable().FirstOrDefault(); using (new WaitCursor()) { Dssp2StepSession dsspSession; var signProps = new SignatureRequestProperties(); signProps.SignerRole = this.Role.Text; signProps.SignatureProductionPlace = this.Location.Text; using (Stream input = File.OpenRead(FilePath.Text)) { var inDoc = new Document() { MimeType = "application/pdf", Content = input }; dsspSession = await dsspClient.UploadDocumentFor2StepAsync(inDoc, signProps); } dsspSession.Sign(); var outDoc = await dsspClient.DownloadDocumentAsync(dsspSession); using (Stream output = File.Create(FilePath.Text)) { await outDoc.Content.CopyToAsync(output); } } }
public async Task <HttpResponseMessage> Post(string id, [FromBody] FormDataCollection formData) { NameIdentifierType newSigner = null; try { foreach (KeyValuePair <String, String> formField in formData) { if (formField.Key == "SignResponse") { try { //check if the sign response is correct, keep the signer newSigner = sessions[id].ValidateSignResponse(formField.Value); docs[id].Alert = new Alert() { Message = "New signature by " + newSigner.Value, Type = "success" }; //get the session and remove it from the store DsspSession session = sessions.Remove(id); //Download the signed document. Document doc = await dsspClient.DownloadDocumentAsync(session); docs[id].Content = doc.Content; //You should save the signed document about here... //For demo purposes, lets validate the signature. This is purely optional SecurityInfo securityInfo = await dsspClient.VerifyAsync(doc); //Keep some interesting info about the signed document docs[id].TimeStampValidity = securityInfo.TimeStampValidity; docs[id].Signatures = new List <SignInfo>(); foreach (SignatureInfo info in securityInfo.Signatures) { SignInfo i = new SignInfo(); i.Signer = info.SignerSubject; i.SignedOn = info.SigningTime; i.Location = info.SignatureProductionPlace; i.Role = info.SignerRole; docs[id].Signatures.Add(i); } } catch (AuthorizationError ae) { newSigner = ae.AttemptedSigner; docs[id].Alert = new Alert() { Message = "Failed signature attempt by " + ae.AttemptedSigner.Value, Type = "warning" }; sessions.Remove(id); //we can remove now, it is no longer valid } } } if (newSigner == null) { docs[id].Alert = new Alert() { Message = "No new signature found", Type = "danger" }; } } catch (Exception e) { docs[id].Alert = new Alert() { Message = "Internal error: " + e.Message, Type = "danger" }; } //Redirecting back to the main site (via HTML to make sure "Get" is used instead of POST) return(RedirectBack()); }