Beispiel #1
0
        public virtual JsonResult ValidateEncryptionThumbprint(EncryptRequestModel model)
        {
            //System.Threading.Thread.Sleep(5000);
            var propertyName = model.PropertyName(x => x.Thumbprint);

            if (ModelState.IsValidField(propertyName))
            {
                return(Json(true));
            }
            var errorMessage = ModelState[propertyName].Errors.First().ErrorMessage;

            return(Json(errorMessage));
        }
Beispiel #2
0
        public virtual JsonResult Encrypt(EncryptRequestModel model)
        {
            ThrowWhenModelStateIsInvalid();

            var config = CreateConfigXmlDocument(model.Thumbprint, model.XmlInput);

            InitializeProvider(model.Thumbprint);

            // encrypt
            foreach (var node in GetEligibleCryptoNodes(config))
            {
                // look for special nested section to encrypt
                var encryptableNode = FindNestedCryptoNode(node) ?? node;

                // strip off the configProtectionProvider attribute if it exists (do not want to encrypt it)
                if (encryptableNode.Attributes != null && encryptableNode.Attributes["configProtectionProvider"] != null)
                {
                    encryptableNode.Attributes.Remove(encryptableNode.Attributes["configProtectionProvider"]);
                }

                // encrypt the node
                var encryptedNode = _provider.Encrypt(encryptableNode);

                // create a new configProtectionProvider attribute
                var attribute = config.CreateAttribute("configProtectionProvider");
                attribute.Value = "CustomProvider";
                Debug.Assert(encryptableNode.Attributes != null);

                // append the attribute to the node that was encrypted (not the encryption result)
                encryptableNode.Attributes.Append(attribute);

                // nest the encryption result into the node that was encrypted
                encryptableNode.InnerXml = encryptedNode.OuterXml;
            }

            // format and return the encrypted xml
            var encrypted = config.GetFormattedXml();

            return(Json(encrypted));
        }