In
typical ASP.NET websites, you usually distribute your assets with your
website. Although this strategy works great for small websites, it’s
pretty much unmanageable when dealing with larger websites. Do you
really want to redeploy your entire website just because you have a new
Hawaiian shirt in your product range and you need to add an image of it?
There’s a better way. In
this section, we’ll tell you how you can integrate public assets and
private assets (such as purchased MP3 files) with your ASP.NET website.
1. Integrating ASP.NET websites with table-driven BLOB content
Currently, the shirt shop
website displays a list of all the products that you have for sale, but
it doesn’t display pictures of the shirts. Because the data is currently
hardcoded, you could just store the image on the website directly; any
changes to the product line would require you to deploy a new version of
the website.
If you stored the pictures of
the shirts in BLOB storage, you could store the URI of the BLOB in your
external data source. As you add new items to your data source, you can
add the associated BLOB at the same time. To expand on the Hawaiian
Shirt Shop example, you could store an image of a shirt in BLOB storage
as well as add a new shirt to the table. In the table, you would store
all the details of the shirt (the name of the shirt, price, description,
and so on) and its associated URI. Figure 1 shows a representation of this setup.
Because you’re happy
for the images of your Hawaiian shirts to be in the public domain, you
can store the shirt images in a public container and set the URI of your
HTML image tag directly to the public URI. The following code shows how
this image tag might look:
<img src="http://silverlightukstorage.blob.core.windows.net/shirtimages/greenshirt.jpg" />
Using a public container is
fine for storing content that you don’t mind making available to the
world, but it’s not an acceptable solution if you want to serve content
that you want to keep protected.
Returning to the
podcast example, suppose you decided that you wanted only paid members
of your website to be able to download your MP3. In this scenario, you
don’t want to store the file in a public container; you want to store it
in a private container instead.
2. Integrating protected, private content
In
the BLOB management web page, you implemented a download link that
allowed you to serve files stored in a private container to your users
on a public website. Now we’re going to expand that technique to a more
integrated, reusable solution by doing the following things:
Creating an HTTP handler that serves MP3 files from BLOB storage
Registering the HTTP handler
Protecting your handler so that only authorized users can use it
HTTP
handlers let specified types of HTTP requests be handled by some custom
code. In this example, you’re going to build a handler that will
intercept requests for MP3 files and return the MP3 files from BLOB
storage instead of from the local filesystem.
Creating the HTTP Handler
You’re going to build a small
sample that will intercept any incoming requests for MP3 files using an
HTTP handler. Rather than attempting to serve the MP3 files from your
website’s filesystem, the handler will retrieve the files from your
private BLOB storage container and serve them to the user who requested
them. For this task, we’re going to build on the techniques that we used
in this listing. You can either serve the files directly (useful for images) or initiate a Save dialog box.
In this example, you’ll download a file directly. For example, when the
user requests the following URI, they’ll be served the file from BLOB
storage:
www.mypodcastwebsite.com/podcast01.mp3
To implement an HTTP handler, add a new httphandler file to your ASP.NET web project. This handler is called MP3Handler.cs. Listing 1 contains the implementation for MP3Handler.cs.
Listing 1. An MP3 HTTP handler that serves MP3s from BLOB storage
The HTTP handler in listing 9.8 will be called whenever an MP3 file request is intercepted at .
When the request has been intercepted, the name of the .mp3 file that
should be downloaded from BLOB storage from the requested path is
extracted . The handler will then download the requested BLOB from the ChrisOriginals private container at by calling the GetBlob method . After the file is retrieved from BLOB storage, you set the MIME type as MP3
(we’ve hardcoded the MIME type but in a more generic sample you could
retrieve it from the BLOB properties) and then write the file back to
the client.
Registering the HTTP Handler
To register the HTTP handler with your website, add the following line to your web.config file in the handlers section under the system.webServer element:
<add name="MP3Handler" path="*.mp3" verb="GET" type="AzurePlayAreaWeb_WebRole.MP3Handler, AzurePlayAreaWeb_WebRole" resourceType="Unspecified"/>
This code will configure your
server to route any web requests that end in .mp3 to your new handler.
This is a great way to protect assets on your server that would normally
be freely accessible. Your handler could check security permissions,
route the call to virtual storage, or deny the request.
Authorization
If you want to restrict your MP3
files to logged-in users, you can use the built-in ASP.NET
authorization and authentication functionality:
<authorization>
<deny users="?"/>
</authorization>
Now you have an
integrated HTTP handler that can serve protected BLOB files from BLOB
storage to authorized users as if it were part of your website. If your
website were serving the same file to hundreds of users every day, then
continually retrieving the same file from BLOB storage would be
inefficient. What you would need to do in that case
is use BLOB storage and local storage together, to cache requests, so
you wouldn’t need to continually request the same BLOB from BLOB
storage.
Putting BLOBs in local
storage to improve performance is useful for showing you how to
integrate local storage and BLOB storage together. It’s also useful as
an option for integrating content. You can use Shared Access Signatures
to provide the same result. Using Shared Access Signatures is the
preferred approach to protect a BLOB because it’s cheaper and takes load
off your servers.
|