Beispiel #1
0
        public static ICrossDomainPolicy BuildSilverlightPolicy(HttpWebResponse response)
        {
            // return null if no Silverlight policy was found, since we offer a second chance with a flash policy
            if ((response.StatusCode != HttpStatusCode.OK) || !CheckContentType(response.ContentType))
            {
                return(null);
            }

            ICrossDomainPolicy policy = null;

            try
            {
                policy = ClientAccessPolicy.FromStream(response.GetResponseStream());
                if (policy != null)
                {
                    AddPolicy(response.ResponseUri, policy);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("CrossDomainAccessManager caught an exception while reading {0}: {1}",
                                                response.ResponseUri, ex));
                // and ignore.
            }
            return(policy);
        }
        static public bool CheckEndPoint(EndPoint endpoint, SocketClientAccessPolicyProtocol protocol)
        {
            // if needed transform the DnsEndPoint into a usable IPEndPoint
            IPEndPoint ip = (endpoint as IPEndPoint);

            if (ip == null)
            {
                throw new ArgumentException("endpoint");
            }

            // find the policy (cached or to be downloaded) associated with the endpoint
            string             address = ip.Address.ToString();
            ClientAccessPolicy policy  = null;

            if (!socket_policies.TryGetValue(address, out policy))
            {
                policy = CreateForEndPoint(ip, protocol);
                socket_policies.Add(address, policy);
            }

            // no access granted if no policy is available
            if (policy == null)
            {
                return(false);
            }

            // does the policy allows access ?
            return(policy.IsAllowed(ip));
        }
        static public ICrossDomainPolicy FromStream(Stream stream)
        {
            ClientAccessPolicy cap = new ClientAccessPolicy();

            // Silverlight accepts whitespaces before the XML - which is invalid XML
            StreamReader sr = new StreamReader(stream);

            while (Char.IsWhiteSpace((char)sr.Peek()))
            {
                sr.Read();
            }

            XmlReaderSettings policy_settings = new XmlReaderSettings();

            policy_settings.DtdProcessing = DtdProcessing.Ignore;
            using (XmlReader reader = XmlReader.Create(sr, policy_settings))
            {
                reader.MoveToContent();
                if (reader.IsEmptyElement)
                {
                    reader.Skip();
                    return(null);
                }
                reader.ReadStartElement("access-policy", String.Empty);
                for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        throw new XmlException(String.Format("Unexpected access-policy content: {0}", reader.NodeType));
                    }

                    if ((reader.LocalName != "cross-domain-access") || reader.IsEmptyElement)
                    {
                        reader.Skip();
                        continue;
                    }

                    reader.ReadStartElement("cross-domain-access", String.Empty);
                    for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
                    {
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            throw new XmlException(String.Format("Unexpected access-policy content: {0}", reader.NodeType));
                        }

                        if ((reader.Name != "policy") || reader.IsEmptyElement)
                        {
                            reader.Skip();
                            continue;
                        }

                        ReadPolicyElement(reader, cap);
                    }
                    reader.ReadEndElement();
                }
                reader.ReadEndElement();
            }
            return(cap);
        }
		static public ICrossDomainPolicy FromStream (Stream stream)
		{
			ClientAccessPolicy cap = new ClientAccessPolicy ();

			// Silverlight accepts whitespaces before the XML - which is invalid XML
			StreamReader sr = new StreamReader (stream);
			while (Char.IsWhiteSpace ((char) sr.Peek ()))
				sr.Read ();

			XmlReaderSettings policy_settings = new XmlReaderSettings ();
			policy_settings.DtdProcessing = DtdProcessing.Ignore;
			using (XmlReader reader = XmlReader.Create (sr, policy_settings)) {
				reader.MoveToContent ();
				if (reader.IsEmptyElement) {
					reader.Skip ();
					return null;
				}
				reader.ReadStartElement ("access-policy", String.Empty);
				for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
					if (reader.NodeType != XmlNodeType.Element)
						throw new XmlException (String.Format ("Unexpected access-policy content: {0}", reader.NodeType));

					if ((reader.LocalName != "cross-domain-access") || reader.IsEmptyElement) {
						reader.Skip ();
						continue;
					}

					reader.ReadStartElement ("cross-domain-access", String.Empty);
					for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
						if (reader.NodeType != XmlNodeType.Element)
							throw new XmlException (String.Format ("Unexpected access-policy content: {0}", reader.NodeType));

						if ((reader.Name != "policy") || reader.IsEmptyElement) {
							reader.Skip ();
							continue;
						}

						ReadPolicyElement (reader, cap);
					}
					reader.ReadEndElement ();
				}
				reader.ReadEndElement ();
			}
			return cap;
		}
Beispiel #5
0
        static void ReadPolicyElement(XmlReader reader, ClientAccessPolicy cap)
        {
            if (reader.HasAttributes || reader.IsEmptyElement)
            {
                reader.Skip();
                return;
            }

            var  policy = new AccessPolicy();
            bool valid  = true;

            reader.ReadStartElement("policy", String.Empty);
            for (reader.MoveToContent(); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent())
            {
                if (IsNonElement(reader))
                {
                    reader.Skip();
                    continue;
                }

                switch (reader.LocalName)
                {
                case "allow-from":
                    ReadAllowFromElement(reader, policy);
                    break;

                case "grant-to":
                    ReadGrantToElement(reader, policy);
                    break;

                default:
                    valid = false;
                    reader.Skip();
                    break;
                }
            }

            if (valid)
            {
                cap.AccessPolicyList.Add(policy);
            }
            reader.ReadEndElement();
        }
Beispiel #6
0
        public static ClientAccessPolicy CreateForEndPoint(IPEndPoint endpoint, SocketClientAccessPolicyProtocol protocol)
        {
            Stream s = null;

            switch (protocol)
            {
            case SocketClientAccessPolicyProtocol.Tcp:
                s = GetPolicyStream(endpoint);
                break;

            case SocketClientAccessPolicyProtocol.Http:
                // <quote>It will NOT attempt to download the policy via the custom TCP protocol if the
                // policy check fails.</quote>
                // http://blogs.msdn.com/ncl/archive/2010/04/15/silverlight-4-socket-policy-changes.aspx
                string url = String.Format("http://{0}:80{1}", endpoint.Address.ToString(),
                                           CrossDomainPolicyManager.ClientAccessPolicyFile);
                s = GetPolicyStream(new Uri(url));
                break;
            }

            if ((s == null) || (s.Length == 0))
            {
                return(null);
            }

            ClientAccessPolicy policy = null;

            try
            {
                policy = (ClientAccessPolicy)ClientAccessPolicy.FromStream(s);
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("CrossDomainAccessManager caught an exception while reading {0}: {1}",
                                                endpoint, ex.Message));
                // and ignore.
            }

            return(policy);
        }
		static void ReadPolicyElement (XmlReader reader, ClientAccessPolicy cap)
		{
			if (reader.HasAttributes || reader.IsEmptyElement) {
				reader.Skip ();
				return;
			}

			var policy = new AccessPolicy ();
			bool valid = true;

			reader.ReadStartElement ("policy", String.Empty);
			for (reader.MoveToContent (); reader.NodeType != XmlNodeType.EndElement; reader.MoveToContent ()) {
				if (IsNonElement (reader)) {
					reader.Skip ();
					continue;
				}

				switch (reader.LocalName) {
				case "allow-from":
					ReadAllowFromElement (reader, policy);
					break;
				case "grant-to":
					ReadGrantToElement (reader, policy);
					break;
				default:
					valid = false;
					reader.Skip ();
					break;
				}
			}

			if (valid)
				cap.AccessPolicyList.Add (policy);
			reader.ReadEndElement ();
		}