protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Get an instance of the CadesSignatureStarter class, responsible for receiving the signature elements and start the
                // signature process
                var signatureStarter = Util.GetRestPkiClient().GetCadesSignatureStarter();

                // Set the signature policy
                signatureStarter.SetSignaturePolicy(StandardCadesSignaturePolicies.PkiBrazil.AdrBasica);

                // Optionally, set a SecurityContext to be used to determine trust in the certificate chain
                //signatureStarter.SetSecurityContext(StandardSecurityContexts.PkiBrazil);
                // Note: Depending on the signature policy chosen above, setting the security context may be mandatory (this is not
                // the case for ICP-Brasil policies, which will automatically use the PkiBrazil security context if none is passed)

                // Optionally, set whether the content should be encapsulated in the resulting CMS. If this parameter is ommitted,
                // the following rules apply:
                // - If no CmsToSign is given, the resulting CMS will include the content
                // - If a CmsToCoSign is given, the resulting CMS will include the content if and only if the CmsToCoSign also includes the content
                signatureStarter.SetEncapsulateContent(true);

                UserFile = Request.QueryString["userfile"];
                CmsFile  = Request.QueryString["cmsfile"];
                if (!String.IsNullOrEmpty(UserFile))
                {
                    // If the user was redirected here by Upload (signature with file uploaded by user), the "userfile" URL argument
                    // will contain the filename under the "App_Data" folder.
                    signatureStarter.SetFileToSign(Server.MapPath("~/App_Data/" + UserFile.Replace("_", ".")));
                }
                else if (!String.IsNullOrEmpty(CmsFile))
                {
                    /*
                     * If the URL argument "cmsfile" is filled, the user has asked to co-sign a previously signed CMS. We'll set the path to the CMS
                     * to be co-signed, which was perviously saved in the App_Data folder by the POST action on this controller. Note two important things:
                     *
                     * 1. The CMS to be co-signed must be set using the method "SetCmsToCoSign", not the method "SetContentToSign" nor "SetFileToSign"
                     *
                     * 2. Since we're creating CMSs with encapsulated content (see call to SetEncapsulateContent above), we don't need to set the content
                     *    to be signed, REST PKI will get the content from the CMS being co-signed.
                     */
                    signatureStarter.SetCmsToCoSign(Server.MapPath("~/App_Data/" + CmsFile.Replace("_", ".")));
                }
                else
                {
                    // If both userfile and cmsfile are null, this is the "signature with server file" case. We'll set the path of the file to be signed
                    signatureStarter.SetFileToSign(Util.GetSampleDocPath());
                }

                // Call the StartWithWebPki() method, which initiates the signature. This yields the token, a 43-character
                // case-sensitive URL-safe string, which identifies this signature process. We'll use this value to call the
                // signWithRestPki() method on the Web PKI component (see javascript on the view) and also to complete the signature
                // on the POST action below (this should not be mistaken with the API access token).
                var token = signatureStarter.StartWithWebPki();

                ViewState["Token"] = token;
            }
        }
예제 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Get an instance of the PadesSignatureStarter class, responsible for receiving the signature elements and start the
                // signature process
                var signatureStarter = Util.GetRestPkiClient().GetPadesSignatureStarter();

                // Set the unit of measurement used to edit the pdf marks and visual representations
                signatureStarter.MeasurementUnits = PadesMeasurementUnits.Centimeters;

                // Set the signature policy
                signatureStarter.SetSignaturePolicy(StandardPadesSignaturePolicies.Basic);

                // Set a SecurityContext to be used to determine trust in the certificate chain
                signatureStarter.SetSecurityContext(StandardSecurityContexts.PkiBrazil);
                // Note: By changing the SecurityContext above you can accept only certificates from a certain PKI,
                // for instance, ICP-Brasil (Lacuna.RestPki.Api.StandardSecurityContexts.PkiBrazil).

                // Set a visual representation for the signature
                signatureStarter.SetVisualRepresentation(new PadesVisualRepresentation()
                {
                    // The tags {{signerName}} and {{signerNationalId}} will be substituted according to the user's certificate
                    // signerName -> full name of the signer
                    // signerNationalId -> if the certificate is ICP-Brasil, contains the signer's CPF
                    Text = new PadesVisualText("Signed by {{signerName}} ({{signerNationalId}})")
                    {
                        // Specify that the signing time should also be rendered
                        IncludeSigningTime = true,

                        // Optionally set the horizontal alignment of the text ('Left' or 'Right'), if not set the default is Left
                        HorizontalAlign = PadesTextHorizontalAlign.Left
                    },

                    // We'll use as background the image in Content/PdfStamp.png
                    Image = new PadesVisualImage(Util.GetPdfStampContent(), "image/png")
                    {
                        // Opacity is an integer from 0 to 100 (0 is completely transparent, 100 is completely opaque).
                        Opacity = 50,

                        // Align the image to the right
                        HorizontalAlign = PadesHorizontalAlign.Right
                    },

                    // Position of the visual representation. We have encapsulated this code in a method to include several
                    // possibilities depending on the argument passed. Experiment changing the argument to see different examples
                    // of signature positioning. Once you decide which is best for your case, you can place the code directly here.
                    Position = PadesVisualElements.GetVisualPositioning(1)
                });

                // If the user was redirected here by Upload (signature with file uploaded by user), the "userfile" URL argument
                // will contain the filename under the "App_Data" folder. Otherwise (signature with server file), we'll sign a sample
                // document.
                UserFile = Request.QueryString["userfile"];
                if (string.IsNullOrEmpty(UserFile))
                {
                    // Set the PDF to be signed as a byte array
                    signatureStarter.SetPdfToSign(Util.GetSampleDocContent());
                }
                else
                {
                    // Set the path of the file to be signed
                    signatureStarter.SetPdfToSign(Server.MapPath("~/App_Data/" + UserFile.Replace("_", ".")));
                }

                /*
                 *      Optionally, add marks to the PDF before signing. These differ from the signature visual representation in that
                 *      they are actually changes done to the document prior to signing, not binded to any signature. Therefore, any number
                 *      of marks can be added, for instance one per page, whereas there can only be one visual representation per signature.
                 *      However, since the marks are in reality changes to the PDF, they can only be added to documents which have no previous
                 *      signatures, otherwise such signatures would be made invalid by the changes to the document (see property
                 *      PadesSignatureStarter.BypassMarksIfSigned). This problem does not occurr with signature visual representations.
                 *
                 *      We have encapsulated this code in a method to include several possibilities depending on the argument passed.
                 *      Experiment changing the argument to see different examples of PDF marks. Once you decide which is best for your case,
                 *      you can place the code directly here.
                 */
                //signatureStarter.PdfMarks.Add(PadesVisualElements.GetPdfMark(1));

                // Call the StartWithWebPki() method, which initiates the signature. This yields the token, a 43-character
                // case-sensitive URL-safe string, which identifies this signature process. We'll use this value to call the
                // signWithRestPki() method on the Web PKI component (see javascript on the view) and also to complete the signature
                // on the POST action below (this should not be mistaken with the API access token).
                var token = signatureStarter.StartWithWebPki();

                ViewState["Token"] = token;
            }
        }