Пример #1
0
        public override CommandResult Bind(ISaml2Message message, ILoggerAdapter logger)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var messageXml = message.ToXml();

            logger?.WriteVerbose("Sending message over Http Redirect Binding\n" + messageXml);

            var serializedRequest = Serialize(messageXml);

            var queryString = message.MessageName + "=" + serializedRequest
                              + (string.IsNullOrEmpty(message.RelayState)
                                  ? ""
                                  : ("&RelayState=" + Uri.EscapeDataString(message.RelayState)));

            if (message.SigningCertificate != null)
            {
                queryString = AddSignature(queryString, message);
            }

            var redirectUri = new Uri(message.DestinationUrl.ToString()
                                      + (string.IsNullOrEmpty(message.DestinationUrl.Query) ? "?" : "&")
                                      + queryString);

            return(new CommandResult()
            {
                HttpStatusCode = HttpStatusCode.SeeOther,
                Location = redirectUri
            });
        }
        public override CommandResult Bind(ISaml2Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var serializedRequest = Serialize(message.ToXml());

            var queryString = message.MessageName + "=" + serializedRequest
                + (string.IsNullOrEmpty(message.RelayState) ? ""
                    : ("&RelayState=" + Uri.EscapeDataString(message.RelayState)));

            if(message.SigningCertificate != null)
            {
                queryString = AddSignature(queryString, message.SigningCertificate);
            }

            var redirectUri = new Uri(message.DestinationUrl.ToString()
                + (String.IsNullOrEmpty(message.DestinationUrl.Query) ? "?" : "&")
                + queryString);

            return new CommandResult()
            {
                HttpStatusCode = HttpStatusCode.SeeOther,
                Location = redirectUri
            };
        }
Пример #3
0
        private static string Serialize(ISaml2Message request)
        {
            using (var compressed = new MemoryStream())
            {
                using (var writer = new StreamWriter(new DeflateStream(compressed, CompressionLevel.Optimal, true)))
                {
                    writer.Write(request.ToXml());
                }

                return HttpUtility.UrlEncode(Convert.ToBase64String(compressed.GetBuffer()));
            }
        }
        private static string Serialize(ISaml2Message request)
        {
            using (var compressed = new MemoryStream())
            {
                using (var writer = new StreamWriter(new DeflateStream(compressed, CompressionLevel.Optimal, true)))
                {
                    writer.Write(request.ToXml());
                }

                return(HttpUtility.UrlEncode(Convert.ToBase64String(compressed.GetBuffer())));
            }
        }
Пример #5
0
        /// <summary>
        /// Binds a message to a binding
        /// </summary>
        /// <param name="binding"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static CommandResult Bind(this Saml2Binding binding, ISaml2Message message)
        {
            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            return(binding.Bind(message.ToXml(), message.DestinationUrl, message.MessageName));
        }
Пример #6
0
        /// <summary>
        /// Binds a message to a binding
        /// </summary>
        /// <param name="binding"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static CommandResult Bind(this Saml2Binding binding, ISaml2Message message)
        {
            if (binding == null)
            {
                throw new ArgumentNullException(nameof(binding));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            return binding.Bind(message.ToXml(), message.DestinationUrl, message.MessageName);
        }
Пример #7
0
        public override CommandResult Bind(ISaml2Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var xml = message.ToXml();

            if (message.SigningCertificate != null)
            {
                var xmlDoc = new XmlDocument()
                {
                    PreserveWhitespace = true
                };

                xmlDoc.LoadXml(xml);

                if (xmlDoc.FirstChild.LastChild.Name.Equals("saml2:Assertion"))
                {
                    ((XmlElement)xmlDoc.FirstChild.LastChild).Sign(message.SigningCertificate, false, message.SigningAlgorithm);
                }
                else
                {
                    xmlDoc.Sign(message.SigningCertificate, true, message.SigningAlgorithm);
                }

                xml = xmlDoc.OuterXml;
            }

            var encodedXml = Convert.ToBase64String(Encoding.UTF8.GetBytes(xml));

            var relayStateHtml = string.IsNullOrEmpty(message.RelayState) ? null
                : string.Format(CultureInfo.InvariantCulture, PostHtmlRelayStateFormatString, message.RelayState);

            var cr = new CommandResult()
            {
                ContentType = "text/html",
                Content     = String.Format(
                    CultureInfo.InvariantCulture,
                    PostHtmlFormatString,
                    message.DestinationUrl,
                    relayStateHtml,
                    message.MessageName,
                    encodedXml)
            };

            return(cr);
        }
Пример #8
0
        public ActionResult Index()
        {
            var request = XElement.Load(Request.InputStream);

            var artifact = request
                           .Element(Saml2Namespaces.SoapEnvelope + "Body")
                           .Element(Saml2Namespaces.Saml2P + "ArtifactResolve")
                           .Element(Saml2Namespaces.Saml2P + "Artifact")
                           .Value;

            var requestId = request
                            .Element(Saml2Namespaces.SoapEnvelope + "Body")
                            .Element(Saml2Namespaces.Saml2P + "ArtifactResolve")
                            .Attribute("ID").Value;

            var binaryArtifact = Convert.FromBase64String(artifact);

            ISaml2Message message = null;

            if (!Saml2ArtifactBinding.PendingMessages.TryRemove(binaryArtifact, out message))
            {
                throw new InvalidOperationException("Unknown artifact");
            }

            var xml = message.ToXml();

            if (message.SigningCertificate != null)
            {
                var xmlDoc = new XmlDocument()
                {
                    PreserveWhitespace = true
                };

                xmlDoc.LoadXml(xml);
                xmlDoc.Sign(message.SigningCertificate, true);
                xml = xmlDoc.OuterXml;
            }

            var response = string.Format(
                CultureInfo.InvariantCulture,
                ResponseFormatString,
                new Saml2Id().Value,
                requestId,
                DateTime.UtcNow.ToSaml2DateTimeString(),
                xml);

            return(Content(response));
        }
Пример #9
0
        public override CommandResult Bind(ISaml2Message message)
        {
            if(message == null)
            {
                throw new ArgumentNullException("message");
            }

            var encodedXml = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(message.ToXml()));

            var cr = new CommandResult()
            {
                Content = String.Format(CultureInfo.InvariantCulture, PostHtmlFormatString,
                message.DestinationUri, message.MessageName, encodedXml)
            };

            return cr;
        }
Пример #10
0
        public override CommandResult Bind(ISaml2Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var encodedXml = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(message.ToXml()));

            var cr = new CommandResult()
            {
                Content = String.Format(CultureInfo.InvariantCulture, PostHtmlFormatString,
                                        message.DestinationUri, message.MessageName, encodedXml)
            };

            return(cr);
        }
Пример #11
0
        public override CommandResult Bind(ISaml2Message message, ILoggerAdapter logger)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var xml = message.ToXml();

            if (message.SigningCertificate != null)
            {
                var xmlDoc = new XmlDocument()
                {
                    PreserveWhitespace = true
                };

                xmlDoc.LoadXml(xml);

                xmlDoc.Sign(message.SigningCertificate, true, message.SigningAlgorithm);
                xml = xmlDoc.OuterXml;
            }

            logger?.WriteVerbose("Sending message over Http POST binding\n" + xml);

            var encodedXml = Convert.ToBase64String(Encoding.UTF8.GetBytes(xml));

            var relayStateHtml = string.IsNullOrEmpty(message.RelayState) ? null
                : string.Format(CultureInfo.InvariantCulture, PostHtmlRelayStateFormatString, message.RelayState);

            var cr = new CommandResult()
            {
                ContentType = "text/html",
                Content     = String.Format(
                    CultureInfo.InvariantCulture,
                    PostHtmlFormatString,
                    message.DestinationUrl,
                    relayStateHtml,
                    message.MessageName,
                    encodedXml)
            };

            return(cr);
        }
Пример #12
0
        public override CommandResult Bind(ISaml2Message message)
        {
            if(message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            var xml = message.ToXml();
            if(message.SigningCertificate != null)
            {
                var xmlDoc = new XmlDocument()
                {
                    PreserveWhitespace = true
                };

                xmlDoc.LoadXml(xml);
                xmlDoc.Sign(message.SigningCertificate, true);
                xml = xmlDoc.OuterXml;
            }

            var encodedXml = Convert.ToBase64String(Encoding.UTF8.GetBytes(xml));

            var relayStateHtml = string.IsNullOrEmpty(message.RelayState) ? null
                : string.Format(CultureInfo.InvariantCulture, PostHtmlRelayStateFormatString, message.RelayState);

            var cr = new CommandResult()
            {
                ContentType = "text/html",
                Content = String.Format(
                    CultureInfo.InvariantCulture,
                    PostHtmlFormatString,
                    message.DestinationUrl,
                    relayStateHtml,
                    message.MessageName,
                    encodedXml)
            };

            return cr;
        }