Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public static SamlResponseResult CheckAndGetUserIDResponseDoc(XmlDocument xmlDoc)
        {
            SamlResponseResult result = ValidateAndGetUserIDResponseDoc(xmlDoc);

            result.ValidateResult.FalseThrow("W3认证返回的结果验证不通过");

            return(result);
        }
Exemple #2
0
        public static SamlResponseResult ValidateAndGetUserIDResponseDoc(XmlDocument xmlDoc)
        {
            SamlResponseResult result = new SamlResponseResult();

            xmlDoc.NullCheck("xmlDoc");

            string userID = string.Empty;

            XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);

            ns.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
            ns.AddNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
            ns.AddNamespace("x", "http://www.w3.org/2000/09/xmldsig#");

            XmlElement signatureElem = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("//x:Signature", ns);

            if (signatureElem != null)
            {
                XmlElement assertionNode = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("saml:Assertion", ns);

                if (assertionNode != null)
                {
                    SignedXml signedXml = new SignedXml(assertionNode);

                    signedXml.LoadXml(signatureElem);

                    X509Certificate2 certificate = GetEmbededPublicCertificate();

                    result.ValidateResult = signedXml.CheckSignature(certificate, true);

                    result.UserID    = assertionNode.GetSingleNodeText("saml:Subject/saml:NameID", ns);
                    result.ReturnUrl = assertionNode.GetSingleNodeText("saml:AttributeStatement/saml:Attribute[@Name='source']/saml:AttributeValue", ns);
                }
            }

            return(result);
        }
Exemple #3
0
        public static SamlResponseResult ValidateAndGetUserIDResponseDoc(XmlDocument xmlDoc)
        {
            SamlResponseResult result = new SamlResponseResult();

            xmlDoc.NullCheck("xmlDoc");

            string userID = string.Empty;

            XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
            ns.AddNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
            ns.AddNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");
            ns.AddNamespace("x", "http://www.w3.org/2000/09/xmldsig#");

            XmlElement signatureElem = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("//x:Signature", ns);

            if (signatureElem != null)
            {
                XmlElement assertionNode = (XmlElement)xmlDoc.DocumentElement.SelectSingleNode("saml:Assertion", ns);

                if (assertionNode != null)
                {
                    SignedXml signedXml = new SignedXml(assertionNode);

                    signedXml.LoadXml(signatureElem);

                    X509Certificate2 certificate = GetEmbededPublicCertificate();

                    result.ValidateResult = signedXml.CheckSignature(certificate, true);

                    result.UserID = assertionNode.GetSingleNodeText("saml:Subject/saml:NameID", ns);
                    result.ReturnUrl = assertionNode.GetSingleNodeText("saml:AttributeStatement/saml:Attribute[@Name='source']/saml:AttributeValue", ns);
                }
            }

            return result;
        }