Exemplo n.º 1
0
        /// <summary>
        /// Возвращает информацию о статусе подписания.
        /// </summary>
        /// <param name="operationId">ИД операции.</param>
        /// <returns>Информация о статусе операции.</returns>
        public SignStatus GetSignStatus(string operationId)
        {
            var hasResult = RequestResultsStorage.TryGetSignRequestResult(operationId, out var signResult);

            if (!hasResult)
            {
                return(new SignStatus(SignRequestState.Failed, $"Invalid param \"{nameof(operationId)}\""));
            }

            var result = signResult.signature != null
        ? new SignStatus(signResult.signature)
        : new SignStatus(SignRequestState.InProgress, "Operation in progress...");

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Подписывает документ.
        /// </summary>
        /// <param name="userId">ИД пользователя.</param>
        /// <param name="seanceId">ИД сеанса (клиентского подключения).</param>
        /// <param name="certificateId">ИД сертификата.</param>
        /// <param name="document">Документ.</param>
        /// <param name="data">Подписываемые данные.</param>
        /// <param name="contentType">Тип подписываемых данных.</param>
        /// <returns>ИД операции.</returns>
        public string SignDocument(int userId, int seanceId, int certificateId, DocumentModel document,
                                   Stream data, ContentType contentType)
        {
            byte[] signature;

            using (var certificate = CertificateManager.GetUserCertificate(userId, certificateId))
            {
                using (var ms = new MemoryStream())
                {
                    data.CopyTo(ms);

                    var contentInfo = new ContentInfo(ms.ToArray());
                    var signedCms   = new SignedCms(contentInfo, true);
                    var cmsSigner   = new CmsSigner(certificate);

                    cmsSigner.IncludeOption        = X509IncludeOption.EndCertOnly;
                    cmsSigner.SignerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
                    cmsSigner.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.Now));

                    signedCms.ComputeSignature(cmsSigner, true);
                    signature = signedCms.Encode();
                }
            }

            var newOperationId = Guid.NewGuid().ToString();

            RequestResultsStorage.SaveRequestResult(userId, newOperationId, null);

            // Имитируем работу.
            Task.Delay(TimeSpan.FromSeconds(10))
            .ContinueWith(t =>
            {
                RequestResultsStorage.SaveRequestResult(userId, newOperationId, signature);
            });

            return(newOperationId);
        }