public static string SerializeToQueryString(OIDClientSerializableMessage obj)
        {
            string queryString = "";
            Dictionary <string, object> data = SerializeToDictionary(obj);

            foreach (KeyValuePair <string, object> entry in data)
            {
                string value = entry.Value.ToString();
                if (typeof(OIDClientSerializableMessage).IsAssignableFrom(entry.Value.GetType()))
                {
                    OIDClientSerializableMessage propertyValue = (OIDClientSerializableMessage)entry.Value;
                    value = SerializeToQueryString(propertyValue);
                }
                else if (entry.Value.GetType().IsGenericType)
                {
                    dynamic dValue = entry.Value;

                    if (GetType(entry.Value.GetType()) == typeof(List <>))
                    {
                        value = "";
                        foreach (object val in dValue)
                        {
                            value += (value == "") ? "" : " ";
                            if (val.GetType() == typeof(ResponseType))
                            {
                                string                enumval    = ((ResponseType)val).ToString();
                                FieldInfo             fi         = typeof(ResponseType).GetField(enumval);
                                EnumMemberAttribute[] attributes = (EnumMemberAttribute[])fi.GetCustomAttributes(typeof(EnumMemberAttribute), false);
                                value += (attributes.Length > 0) ? attributes[0].Value : enumval;
                            }
                            else if (val.GetType() == typeof(MessageScope))
                            {
                                string                enumval    = ((MessageScope)val).ToString();
                                FieldInfo             fi         = typeof(MessageScope).GetField(enumval);
                                EnumMemberAttribute[] attributes = (EnumMemberAttribute[])fi.GetCustomAttributes(typeof(EnumMemberAttribute), false);
                                value += (attributes.Length > 0) ? attributes[0].Value : enumval;
                            }
                            else
                            {
                                value += val.ToString();
                            }
                        }
                    }
                    else if (GetType(entry.Value.GetType()) == typeof(Dictionary <,>))
                    {
                        value = "{ ";
                        foreach (string val in dValue.Keys)
                        {
                            value += "\"" + val + "\": " + SerializeToJsonString(dValue[val]) + ",";
                        }
                        value  = value.TrimEnd(',');
                        value += " }";
                    }
                }

                queryString += entry.Key + "=" + Uri.EscapeDataString(value) + "&";
            }

            return(queryString.TrimEnd('&'));
        }
Пример #2
0
        /// <summary>
        /// Method that performs an HTTP POST and returns the Json deserialization
        /// of the content returned from the call.
        /// </summary>
        /// <param name="webRequest">The WebRequest object to be used for the call.</param>
        /// <param name="message">The message to be passed as content of the call.</param>
        /// <param name="json">A flag indicating whether the message has a Json format or not.
        /// In the first case the message is posted as a serialization of the Json.
        /// In the second case the messge is serialized to query string.</param>
        /// <returns>Json deserialization of the content returned from the call.</returns>
        public static string PostUrlContent(WebRequest webRequest, OIDClientSerializableMessage message, bool json = false)
        {
            ((HttpWebRequest)webRequest).CookieContainer = cookies;
            webRequest.Method = "POST";

            string postData = "";

            if (message != null)
            {
                if (json)
                {
                    postData = message.SerializeToJsonString();
                }
                else
                {
                    postData = message.SerializeToQueryString();
                }
            }
            byte[] postBytes = Encoding.UTF8.GetBytes(postData);

            webRequest.ContentType   = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postBytes.Length;

            Stream postStream = webRequest.GetRequestStream();

            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Close();

            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

            StreamReader rdr = new StreamReader(response.GetResponseStream());

            return(rdr.ReadToEnd());
        }
        private static void ParseMessageScope(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            if (value.GetType() == typeof(MessageScope))
            {
                p.SetValue(obj, (MessageScope)value);
            }
            else
            {
                switch (value.ToString())
                {
                case "openid":
                    p.SetValue(obj, MessageScope.Openid);
                    break;

                case  "profile":
                    p.SetValue(obj, MessageScope.Profile);
                    break;

                case  "email":
                    p.SetValue(obj, MessageScope.Email);
                    break;

                case  "address":
                    p.SetValue(obj, MessageScope.Address);
                    break;

                case "phone":
                    p.SetValue(obj, MessageScope.Phone);
                    break;

                default:
                    break;
                }
            }
        }
        private static void ParseResponseType(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            if (value.GetType() == typeof(ResponseType))
            {
                p.SetValue(obj, (ResponseType)value);
            }
            else
            {
                switch (value.ToString())
                {
                case "code":
                    p.SetValue(obj, ResponseType.Code);
                    break;

                case "token":
                    p.SetValue(obj, ResponseType.Token);
                    break;

                case "id_token":
                    p.SetValue(obj, ResponseType.Token);
                    break;

                default:
                    break;
                }
            }
        }
        private static void ParseListOIDCKey(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            List <OIDCKey> propertyValue = new List <OIDCKey>();

            if (value.GetType() == typeof(OIDCKey))
            {
                propertyValue.Add((OIDCKey)value);
            }
            else if (value.GetType() == typeof(List <OIDCKey>))
            {
                propertyValue = (List <OIDCKey>)value;
            }
            else if (value.GetType() == typeof(string))
            {
                ParseOIDCMessage(obj, p, value);
            }
            else
            {
                JArray arrayData = (JArray)value;
                foreach (JValue val in arrayData)
                {
                    propertyValue.Add(val.ToObject <OIDCKey>());
                }
            }

            p.SetValue(obj, propertyValue);
        }
Пример #6
0
        /// <summary>
        /// Method that performs an HTTP POST and returns the Json deserialization
        /// of the content returned from the call.
        /// </summary>
        /// <param name="webRequest">The WebRequest object to be used for the call.</param>
        /// <param name="message">The message to be passed as content of the call.</param>
        /// <param name="json">A flag indicating whether the message has a Json format or not.
        /// In the first case the message is posted as a serialization of the Json.
        /// In the second case the messge is serialized to query string.</param>
        /// <returns>Json deserialization of the content returned from the call.</returns>
        public Dictionary <string, object> PostUrlContent(WebRequest webRequest, OIDClientSerializableMessage message, bool json = false)
        {
            webRequest.Method = "POST";

            string postData = "";

            if (message != null)
            {
                if (json)
                {
                    postData = message.serializeToJsonString();
                }
                else
                {
                    postData = message.serializeToQueryString();
                }
            }
            byte[] postBytes = Encoding.UTF8.GetBytes(postData);

            webRequest.ContentType   = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postBytes.Length;

            Stream postStream = webRequest.GetRequestStream();

            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Close();

            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

            StreamReader    rdr            = new StreamReader(response.GetResponseStream());
            IJsonSerializer JsonSerializer = new DefaultJsonSerializer();

            return(JsonSerializer.Deserialize <Dictionary <string, object> >(rdr.ReadToEnd()));
        }
        private static void ParseOIDCMessage(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            OIDClientSerializableMessage propertyValue = (OIDClientSerializableMessage)Activator.CreateInstance(p.PropertyType);
            JObject curObj = (JObject)value;

            propertyValue.DeserializeFromDictionary(curObj.ToObject <Dictionary <string, object> >());
            p.SetValue(obj, propertyValue);
        }
        private static void ParseDateTime(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            long     dataLong      = long.Parse(string.Empty + value);
            DateTime propertyValue = DateTime.MaxValue;

            if (dataLong != 0)
            {
                propertyValue = SecondsUtcToDateTime(dataLong);
            }

            p.SetValue(obj, propertyValue);
        }
        private static void ParseListMessageScope(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            List <MessageScope> propertyValue = new List <MessageScope>();

            if (value.GetType() == typeof(MessageScope))
            {
                propertyValue.Add((MessageScope)value);
            }
            else if (value.GetType() == typeof(string))
            {
                switch (value.ToString())
                {
                case "openid":
                    propertyValue.Add(MessageScope.Openid);
                    break;

                case "profile":
                    propertyValue.Add(MessageScope.Profile);
                    break;

                case "email":
                    propertyValue.Add(MessageScope.Email);
                    break;

                case "address":
                    propertyValue.Add(MessageScope.Address);
                    break;

                case "phone":
                    propertyValue.Add(MessageScope.Phone);
                    break;

                default:
                    break;
                }
            }
            else
            {
                JArray arrayData = (JArray)value;
                foreach (JValue val in arrayData)
                {
                    if (!new List <string> {
                        "openid", "profile", "email", "address", "phone"
                    }.Contains(val.ToString()))
                    {
                        continue;
                    }
                    propertyValue.Add(val.ToObject <MessageScope>());
                }
            }

            p.SetValue(obj, propertyValue);
        }
        private static void ParseListString(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            List <string> propertyValue = new List <string>();

            if (value.GetType() == typeof(string))
            {
                propertyValue.Add((string)value);
            }
            else
            {
                dynamic arrayData = value;
                foreach (string val in arrayData)
                {
                    propertyValue.Add(val);
                }
            }

            p.SetValue(obj, propertyValue);
        }
        public static void DeserializeFromDictionary(OIDClientSerializableMessage obj, Dictionary <string, object> data)
        {
            PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo p in properties)
            {
                string propertyCamel      = p.Name;
                string propertyUnderscore = Regex.Replace(propertyCamel, "(?<=.)([A-Z])", "_$0", RegexOptions.Compiled).ToLower();
                Type   propertyType       = p.PropertyType;
                object propertyValue      = (data.ContainsKey(propertyUnderscore)) ? data[propertyUnderscore] : null;

                if (!ParsersPerType.ContainsKey(p.PropertyType) || propertyValue == null)
                {
                    continue;
                }

                Delegate d = ParsersPerType[propertyType];
                d.DynamicInvoke(obj, p, propertyValue);
            }
        }
        public static void DeserializeFromQueryString(OIDClientSerializableMessage obj, string query)
        {
            string queryString = query;

            if (queryString.StartsWith("?"))
            {
                queryString = queryString.Substring(1);
            }

            Dictionary <string, object> data = new Dictionary <string, object>();

            foreach (string param in queryString.Split('&'))
            {
                string[] vals = param.Split('=');
                data.Add(vals[0], Uri.UnescapeDataString(vals[1]));
            }

            if (!data.ContainsKey("error"))
            {
                DeserializeFromDictionary(obj, data);
            }
        }
        private static void ParseInteger(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            int propertyValue = int.Parse(string.Empty + value);

            p.SetValue(obj, propertyValue);
        }
        public static string SerializeToJsonString(OIDClientSerializableMessage obj)
        {
            Dictionary <string, object> data = SerializeToDictionary(obj);

            return(SerializeToJsonString(data));
        }
        private static object ParseOIDCMessage(object value)
        {
            OIDClientSerializableMessage propertyValue = (OIDClientSerializableMessage)value;

            return(propertyValue.SerializeToDictionary());
        }
        private static void ParseDictionaryStringObject(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            Dictionary <string, object> propertyValue = (Dictionary <string, object>)value;

            p.SetValue(obj, propertyValue);
        }
        private static void ParseBool(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            bool propertyValue = bool.Parse(string.Empty + value);

            p.SetValue(obj, propertyValue);
        }
        private static void ParseString(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            string propertyValue = (string)value;

            p.SetValue(obj, propertyValue);
        }
        private static void ParseListResponseType(OIDClientSerializableMessage obj, PropertyInfo p, object value)
        {
            List <ResponseType> propertyValue = new List <ResponseType>();

            if (value.GetType() == typeof(ResponseType))
            {
                propertyValue.Add((ResponseType)value);
            }
            else if (value.GetType() == typeof(string))
            {
                switch (value.ToString())
                {
                case "code":
                    propertyValue.Add(ResponseType.Code);
                    break;

                case "token":
                    propertyValue.Add(ResponseType.Token);
                    break;

                case "id_token":
                    propertyValue.Add(ResponseType.IdToken);
                    break;

                default:
                    break;
                }
            }
            else if (value.GetType() == typeof(List <ResponseType>))
            {
                List <ResponseType> arrayData = (List <ResponseType>)value;
                foreach (ResponseType val in arrayData)
                {
                    propertyValue.Add(val);
                }
            }
            else
            {
                JArray arrayData = (JArray)value;
                foreach (JValue val in arrayData)
                {
                    switch (val.ToString())
                    {
                    case "code":
                        propertyValue.Add(ResponseType.Code);
                        break;

                    case "token":
                        propertyValue.Add(ResponseType.Token);
                        break;

                    case "id_token":
                        propertyValue.Add(ResponseType.IdToken);
                        break;

                    default:
                        break;
                    }
                }
            }

            p.SetValue(obj, propertyValue);
        }