Click here to Skip to main content
15,885,914 members
Articles / Hosted Services / Azure
Technical Blog

Azure: Crossdomain.xml inside Root of Blob Storage

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Feb 2020CPOL1 min read 7.8K  
How to implement a crossdomain.xml file to allow the Flash client to connect to my site
My recent Azure adventures are working with a Flash client as the front end, which meant the need to implement a crossdomain.xml file to allow the Flash client to connect to my site. This post will outline the problem and give a solution to how this can be done.

What is Cross-Domain?

A cross-domain policy file is an XML document that grants a web client, such as Adobe Flash Player or Silverlight permission to handle data across domains. When a client hosts content from a particular source domain and that content makes requests directed towards a domain other than its own, the remote domain needs to host a cross-domain policy file that grants access to the source domain, allowing the client to continue the transaction.

And in Azure, this is all fine for the hosted service/site. You can just create the XML file within your site and you’re ready to rock and roll.

The Problem

The same thing must be done on your Blob storage account to allow your web client to access videos or other assets that are hosted in your Blob storage. From initial looks at how Blob storage works, it requires a container to place files/blobs into. But after a little digging, I got to the root (hehe) of the solution.

By creating a container called “$root”, this mimics the root directory and as far as a web client is concerned, it exists within the root of your blob storage URI (or whatever the URI of the CNAME you have pointed to your blob storage).

The Solution

I like to setup everything necessary in code to create/run my Azure projects. So if you add the following method into the Run method of your web/worker role, it will automatically create the root folder and your defined crossdomain.xml file.

C#
private void CreateCrossDomainFile()
{
	CloudBlobClient client = _cloudStorageAccount.CreateCloudBlobClient();

	client.GetContainerReference("$root").CreateIfNotExist();
	client.GetContainerReference("$root").SetPermissions(
    new BlobContainerPermissions 
    { 
      PublicAccess = BlobContainerPublicAccessType.Blob 
    });
	CloudBlob blob = client.GetBlobReference("crossdomain.xml");
	blob.Properties.ContentType = "text/xml";
	blob.UploadText(@"<?xml version=""1.0"" encoding=""utf-8""?>
    <cross-domain-policy>
    <allow-access-from domain=""www.YOURDOMAIN.co.uk"" secure=""false"" /> 
    <allow-access-from domain=""YOURDOMAIN.co.uk"" secure=""false"" />   
    </cross-domain-policy>");
}

Inside

C#
public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        CreateCrossDomainFile();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
I have been working in software development for over 16 years, during that time I have worn many hats.

I have worked as a Software Engineer, Architect, Agile Coach and Trainer. I’ve created teams, I’ve lead teams, but my main goal is to help teams build great software and enjoy the process.

I help a whole range of businesses – from startups with just an idea who want to build a team to take that idea into reality and FTSE 100 businesses who need to optimise existing teams – I train, mentor and coach them to success.

If you happen to know of anybody who could benefit from results like this, then please go to my contact page and get in touch.

Owen Davies

Comments and Discussions

 
-- There are no messages in this forum --