Exemplo n.º 1
0
        private static XmlElement toXmlElement(PaymentContextType paymentContext)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add("samlp", Saml2Constants.SAML2_PROTOCOL_NAMESPACE);
            ns.Add("saml", Saml2Constants.SAML2_ASSERTION_NAMESPACE);
            ns.Add("xs", "http://www.w3.org/2001/XMLSchema");

            XmlRootAttribute xRoot = new XmlRootAttribute();

            xRoot.ElementName = "PaymentContext";
            xRoot.Namespace   = Saml2Constants.SAML2_ASSERTION_NAMESPACE;
            XmlSerializer serializer    = new XmlSerializer(typeof(PaymentContextType), xRoot);
            MemoryStream  memoryStream  = new MemoryStream();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            serializer.Serialize(xmlTextWriter, paymentContext, ns);

            XmlDocument document = new XmlDocument();

            memoryStream.Seek(0, SeekOrigin.Begin);
            document.Load(memoryStream);

            foreach (XmlNode node in document.ChildNodes)
            {
                if (node is XmlElement)
                {
                    return((XmlElement)node);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates a SAML v2.0 Authentication Request with HTTP Browser Post Binding.
        /// The return string containing the request is NOT Base64 encoded.
        /// </summary>
        /// <param name="linkIDContext">the linkID authentication/payment configuration</param>
        /// <returns>SAML request</returns>
        public static AuthnRequestType generateAuthnRequest(LinkIDAuthenticationContext linkIDContext)
        {
            AuthnRequestType authnRequest = new AuthnRequestType();

            authnRequest.ForceAuthn   = true;
            authnRequest.ID           = Guid.NewGuid().ToString();
            authnRequest.Version      = "2.0";
            authnRequest.IssueInstant = DateTime.UtcNow;

            NameIDType issuer = new NameIDType();

            issuer.Value        = linkIDContext.applicationName;
            authnRequest.Issuer = issuer;

            NameIDPolicyType nameIdPolicy = new NameIDPolicyType();

            nameIdPolicy.AllowCreate          = true;
            nameIdPolicy.AllowCreateSpecified = true;
            authnRequest.NameIDPolicy         = nameIdPolicy;

            Dictionary <string, string> deviceContextMap = linkIDContext.getDeviceContextMap();
            DeviceContextType           deviceContext    = null;

            if (null != deviceContextMap && deviceContextMap.Count > 0)
            {
                deviceContext = new DeviceContextType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string deviceContextKey in deviceContextMap.Keys)
                {
                    string        deviceContextValue = deviceContextMap[deviceContextKey];
                    AttributeType attribute          = new AttributeType();
                    attribute.Name           = deviceContextKey;
                    attribute.AttributeValue = new object[] { deviceContextValue };
                    attributes.Add(attribute);
                    deviceContext.Items = attributes.ToArray();
                }
            }
            SubjectAttributesType subjectAttributes = null;

            if (null != linkIDContext.attributeSuggestions && linkIDContext.attributeSuggestions.Count > 0)
            {
                subjectAttributes = new SubjectAttributesType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string attributeName in linkIDContext.attributeSuggestions.Keys)
                {
                    List <object> values = linkIDContext.attributeSuggestions[attributeName];

                    AttributeType attribute = new AttributeType();
                    attribute.Name           = attributeName;
                    attribute.AttributeValue = values.ToArray();
                    attributes.Add(attribute);
                    subjectAttributes.Items = attributes.ToArray();
                }
            }

            PaymentContextType paymentContextType = null;

            if (null != linkIDContext.paymentContext)
            {
                Dictionary <String, String> paymentContextDict = linkIDContext.paymentContext.toDictionary();
                paymentContextType = new PaymentContextType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string paymentContextKey in paymentContextDict.Keys)
                {
                    string        value     = paymentContextDict[paymentContextKey];
                    AttributeType attribute = new AttributeType();
                    attribute.Name           = paymentContextKey;
                    attribute.AttributeValue = new object[] { value };
                    attributes.Add(attribute);
                    paymentContextType.Items = attributes.ToArray();
                }
            }

            CallbackType callbackType = null;

            if (null != linkIDContext.callback)
            {
                Dictionary <String, String> callbackDict = linkIDContext.callback.toDictionary();
                callbackType = new CallbackType();
                List <AttributeType> attributes = new List <AttributeType>();
                foreach (string callbackKey in callbackDict.Keys)
                {
                    string        value     = callbackDict[callbackKey];
                    AttributeType attribute = new AttributeType();
                    attribute.Name           = callbackKey;
                    attribute.AttributeValue = new object[] { value };
                    attributes.Add(attribute);
                    callbackType.Items = attributes.ToArray();
                }
            }


            if (null != deviceContext || null != subjectAttributes || null != paymentContextType || null != callbackType)
            {
                ExtensionsType    extensions     = new ExtensionsType();
                List <XmlElement> extensionsList = new List <XmlElement>();
                if (null != subjectAttributes)
                {
                    extensionsList.Add(toXmlElement(subjectAttributes));
                }
                if (null != deviceContext)
                {
                    extensionsList.Add(toXmlElement(deviceContext));
                }
                if (null != paymentContextType)
                {
                    extensionsList.Add(toXmlElement(paymentContextType));
                }
                if (null != callbackType)
                {
                    extensionsList.Add(toXmlElement(callbackType));
                }
                extensions.Any          = extensionsList.ToArray();
                authnRequest.Extensions = extensions;
            }

            return(authnRequest);
        }