Esempio n. 1
0
        public async Task <HttpResponseMessage> Upload()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new PhotoMultipartFormDataStreamProvider(AppSettings.Get <string>("UploadCertPath"));
            var result   = await Request.Content.ReadAsMultipartAsync(provider);

            var certExtension = Path.GetExtension(result.FileData.First().LocalFileName);

            if (certExtension == ".wsdl" || certExtension == ".xml")
            {
                try
                {
                    var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);

                    return(this.Request.CreateResponse(HttpStatusCode.OK, uploadedFileInfo.Name));
                }
                catch (Exception ex)
                {
                    throw new Exception("Неуспешно прикачување на датотека");
                }
            }
            else
            {
                throw new InvalidCertException(certExtension);
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> GetCertPublicKey()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new PhotoMultipartFormDataStreamProvider(AppSettings.Get <string>("UploadCertPath"));
            var result   = await Request.Content.ReadAsMultipartAsync(provider);

            var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);
            var cert             = new X509Certificate2();

            try
            {
                cert = new X509Certificate2(uploadedFileInfo.FullName);
                //StringBuilder builder = new StringBuilder();
                //builder.AppendLine(Convert.ToBase64String(cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
                //var stringBuilder = builder.ToString();
                //RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;
                var publicKey = cert.GetPublicKey();
                //var publicKey = rsa.ToXmlString(false);
                return(Request.CreateResponse(HttpStatusCode.OK, Convert.ToBase64String(publicKey)));
            }
            catch (CryptographicException ex)
            {
                throw new InvalidCertificate(ex.Message);
            }
        }