public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            if (clientCredentials == null || clientCredentials.ClientCertificate.Certificate == null)
            {
                throw new Exception("clientCredentials Certificate is missing");
            }
            string action = null, messageID = "urn:uuid:" + Guid.NewGuid().ToString("D");

            foreach (var head in request.Headers)
            {
                var x = XElement.Parse(head.ToString());
                switch (head.Name)
                {
                case "Action": action = x.Value; break;

                case "MessageID": messageID = x.Value; break;
                }
            }

            MessageBuffer msgbuf = request.CreateBufferedCopy(int.MaxValue);
            var           xdoc   = XDocument.Load(msgbuf.AsStream());

            SealUtilities.CheckAndSetSamlDsPreFix(xdoc);             //Hack

            //Fill header
            NameSpaces.SetMissingNamespaces(xdoc);
            var hd = xdoc.Root.Element(NameSpaces.xsoap + "Header");
            var ac = hd.Element(NameSpaces.xwsa2 + "Action") ?? hd.Element(NameSpaces.xwsa + "Action");
            var md = hd.Element(NameSpaces.xwsa2 + "MessageID") ?? hd.Element(NameSpaces.xwsa + "MessageID");

            hd.Add(new XElement(NameSpaces.xwsa + "Action", new XAttribute("mustUnderstand", "1"), new XAttribute(NameSpaces.xwsu + "Id", "action"), action),
                   new XElement(NameSpaces.xwsa + "MessageID", new XAttribute(NameSpaces.xwsu + "Id", "messageID"), messageID),
                   new XElement(NameSpaces.xwsse + "Security", new XAttribute("mustUnderstand", "1"), new XAttribute(NameSpaces.xwsu + "Id", "security"),
                                new XElement(NameSpaces.xwsu + "Timestamp", new XAttribute(NameSpaces.xwsu + "Id", "timestamp"),
                                             new XElement(NameSpaces.xwsu + "Created", DateTime.UtcNow.ToString("u").Replace(' ', 'T'))
                                             )
                                )
                   );
            ac.Remove();
            if (md != null)
            {
                md.Remove();
            }

            xdoc.Root.Element(NameSpaces.xsoap + "Body").Add(new XAttribute(NameSpaces.xwsu + "Id", "body"));

            var         signer   = new SealSignedXml(xdoc);
            XmlDocument envelope = signer.Sign(clientCredentials.ClientCertificate.Certificate);

            var nrd = new XmlNodeReader(envelope);

            msgbuf  = Message.CreateMessage(nrd, int.MaxValue, request.Version).CreateBufferedCopy(int.MaxValue);
            request = msgbuf.CreateMessage();
            return(envelope);
        }
Esempio n. 2
0
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            var hdidx = request.Headers.FindHeader("Security", NameSpaces.wsse);

            if (hdidx == -1)
            {
                throw new FaultException("Security header is missing");
            }
            var hd = request.Headers[hdidx];

            if (!(hd is SealCardMessageHeader))
            {
                var xdoc = XDocument.Load(request.ToStream());
                SealUtilities.CheckAndSetSamlDsPreFix(xdoc); //Hack
                request = xdoc.ToMessage(request.Version);
            }
            else
            {
                request.Headers.RemoveAt(hdidx);
                request.Headers.Insert(hdidx, hd as SealCardMessageHeader);
            }
            return(null);
        }