SILVERLIGHT CROSS-DOMAIN / CLIENTACCESSPOLICY FILE CHECKER


By Peter Bromberg
Printer Friendly Version
View My Articles

  

A simple page that accepts any url (to a Silverlight app or page), decomposes the uri parts, and checks for either of the accepted cross-domain policy files on the site.



Recently I started putting together a list of search urls that return RSS results and I needed to find out if each particular site supported the required cross-domain policy files. The result was this simple page that does the checking regardless of what url you put in it.

To keep things simple I made it a "script-only" page with no codebehind:

 

<%@ Page Language="C#" AutoEventWireup="true"  %>
<%@ Import Namespace="System.Xml" %>

<script runat="server" language="C#" >

    protected void Button1_Click(object sender, EventArgs e)
    {
        string msg = "";
        var uri = new Uri(TextBox1.Text);
        string host = uri.Host;
        string scheme = uri.Scheme;
        string test1 = scheme + "://" + host + "/crossdomain.xml";
        var doc = new XmlDocument();
        try
        {
            doc.Load(test1);
            msg = doc.OuterXml;
            // clean up for display in the TextArea
            msg = msg.Replace("><", ">\r\n<");
        }
        catch
        {
            msg = "====================================\r\nNo crossdomain.xml policy file\r\n";
        }
        TextBox2.Visible = true;
        TextBox2.Text = msg;

        string test2 = scheme + "://" + host + "/clientaccesspolicy.xml";
        try
        {
            // clean up for display
            doc.Load(test2);
            string test3 = doc.OuterXml;
            // clean up for display in the TextArea
            test3 = test3.Replace("><", ">\r\n<");
            msg = "======================================\r\n" + test3;
        }
        catch
        {
            msg = "====================================\r\nNo clientaccesspolicy.xml policy file";
        }
        TextBox2.Visible = true;
        TextBox2.Text += msg;
    }
    </script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>SILVERLIGHT CROSS-DOMAIN / CLIENTACCESSPOLICY FILE CHECKER</title>
</head>
<body>
    <form id="form1" runat="server">
    <p>
        <br />
        <asp:Label ID="Label2" runat="server" Font-Names="TAHOMA" 
            Text="SILVERLIGHT CROSS-DOMAIN / CLIENTACCESSPOLICY FILE CHECKER"></asp:Label>
    </p>
    <div>    
        <asp:Label ID="Label1" runat="server" Text="URL TO APPLICATION:" 
            Font-Names="Tahoma"></asp:Label>
         &nbsp;
        <asp:TextBox ID="TextBox1" runat="server" Width="250px">http://</asp:TextBox>    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Check" 
            Font-Names="Tahoma" />    
    </div>
    <asp:TextBox ID="TextBox2" runat="server" Height="500px" TextMode="MultiLine" Visible="False" 
        Width="550px"></asp:TextBox>
    </form>
</body>
</html>
The functionality is very simple; first we pull apart the Uri parts so that we can reconstruct a URL to the root of the site, then we attempt to load both the crossdomain.xml and the clientaccesspolicy.xml files. If the load is successful we clean up the XML string in a sort of "poor man's xml formatting" and display the policy file; if it is not found, we display the appropriate message.

You can download a zipped copy of the file here.


Biography
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking ,financial and telephony for 20 years. Pete focuses exclusively on the .NET Platform, and his samples at GotDotNet.com have been downloaded over 56,000 times. Peter enjoys producing 3D raytraced digital photo collage with Maya, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites.
Please post questions at forums, not via email!

button
 
Article Discussion: SILVERLIGHT CROSS-DOMAIN / CLIENTACCESSPOLICY FILE CHECKER
Peter Bromberg posted at 05-Oct-08 09:03
Original Article

 
check the existence first
Wilson Chan replied to Peter Bromberg at 08-Oct-08 09:33
hi, you should check the existence of the file first, for graceful reason:)

 
The code is attempting to load the file over HTTP.
Peter Bromberg replied to Wilson Chan at 08-Oct-08 09:48
The only way to check for the exsitence of a remote file via HTTP is to attempt to load it and catch the exception, if any. The code in the article already does this. Am I missing something, or did you just not read the code?

 
httpRequest
Wilson Chan replied to Peter Bromberg at 08-Oct-08 10:06

hi, usually I will do like the following:

 try
 {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://uri/
ClientAccessPolicy.xml");
            HttpStatusCode status;
            using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
            {
                status = request.StatusCode;
            }
            if (status == HttpStatusCode.OK)
            {
  //doc.Load();
            }
            else
            {
            //not found
            }
 }
 catch(Exception)
 {
 //not found
 }

 


 


 
Yah this is all fine, but--
Peter Bromberg replied to Wilson Chan at 09-Oct-08 01:20
I want the user to be able to SEE the Xml so they can examine it. Consequently, XmlDocument.Load takes care of everything in one method call.

promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class
Manipulate file attributes in VB.NET
Forms Based Authentication Filtered Content Editor for SharePoint
How to create a Tree View of the Windows Folder and extract all the file-folder info.
How to use AssemblyInfo.cs file in win forms to provide much needed information on Assemblies
Sorting In Datagrid
Helper class to work with NativeMethods in the native api's
Silverlight Line Of Business Applications With Offline WPF Versions
C# : Database monitoring system using XML file
C# : Adding ComboBox to ListView SubItem
Sum of Numbers Captcha: Keeping it Simple
C# Create a Piechart for the specified Hard Disk Drive Utilization
Extension Methods for DataSet and DataTable that makes tasks easier
Accessing IIS Hosted WCF Services from PHP
Helper class that provides most commonly used Extension Methods for DateTime object
Helper class to work with a Status Bar in WPF.
Finding Unmatched Records in Dataset Tables Using Linq
Silverlight Toolkit: Autocomplete TextBox Stock Symbols and Chart
COOL Auto Complete textbox using javascript
Creating a Serializable Log Entry for Microsoft Enterprise Library to log to a Database
ASP.NET Searching Values in Datagrid