Friday, March 12, 2010

Sharing an Encrypted Authentication Ticket Between ASP.NET 1.1 and ASP.NET 2.0 (or higher)

Thank you to Dan Sellers's WebLog for this little tidbit regarding sharing an encrypted Authentication ticket between a .NET 1.1 application and .NET 2.0 (or higher). I am posting it here more for my benefit as it seems I've been doing this a lot lately. You can read up on the details on his site, but the important bit is that the first machine key section goes in the .net 1.1 app and the second one goes in the .net 2.0 or higher app. Poof. Magic. Like anything else, really simple if you know how to do it. Hope this helps.


Snippet of the machine config for an ASP.NET 1.1 Application:


<machineKey
validationKey='5C9D7A8F3E336275166075E596F19EB9B478F771C7FE45B65BF6E9B41BA9575F04672CCC4242B2245200CD0E63A8991CA6BFB2D77FE9C5B0D69889359574C5F3'
decryptionKey='AF96F355CEC57EFD2F996515BF465DD399FAF7B806B2CD55' validation='SHA1'/>

Snippet of Web Config for an ASP.NET 2.0 Application with decryption attribute specified:


<system.web>
<machineKey
validationKey='5C9D7A8F3E336275166075E596F19EB9B478F771C7FE45B65BF6E9B41BA9575F04672CCC4242B2245200CD0E63A8991CA6BFB2D77FE9C5B0D69889359574C5F3'
decryptionKey='AF96F355CEC57EFD2F996515BF465DD399FAF7B806B2CD55'
validation='SHA1' decryption='3DES' />

Wednesday, December 30, 2009

Silverlight Templates: Hot Sheet

Just added the 'Hot Sheet' for silverlight developers at SilverlightTemplates.com. So now, Registered designers can have their contact info listed on our hot sheet. All you need to do is register and upload at least one template. Check it out at http://www.silverlighttemplates.com/Home/Developers.

Wednesday, December 16, 2009

Getting Started With SilverlightTemplates

Check out SilverlightTemplates.com and its Getting Started Guide for Silverlight designers. If you follow this link: Getting Started it should give you all the information you need to start using the site.

The intent with SilverlightTemplates.com is to give Silverlight designers/developers a community portal to get their work out in front of as large an audience as possible. To that end, we are working hard with bloggers, user groups and other commercial sites to spread the word. If you belong to a Silverlight-oriented group, please spread the word. They will gladly do a "link exchange" (you know, you link to us and we'll link to you) as this is a great way to build a strong community.

Contact them at Info@SilverlightTemplates.com

Thursday, November 5, 2009

Load a Xap file from Binary

Recently I was faced with a situation where I needed to load a .xap file from a database and preview the file using Silverlight. Typically, to display a Silverlight app hosted in a web site, one would use the
tag. A detailed description of how to use this with all the available parameters can be found here.

The .xap file, which is really just a compressed package of all the necessary bits needed to run a Silverlight application, typically is stored in the file system and is referenced using the src attribute of the tag. So right there is the problem. Our .xap files are not stored in the file system, but rather in binary fields in our SQL Server 2008 database. The src property is a string indicating the path and file name of the .xap package. Argh.

To deal with this, what was needed is a way to reference the .xap file (stored in the database) with a relative url. A Silverlight application has a mime type of application/x-silverlight. This is important to note because of an interesting way one can use an .aspx page to render binary content. Prior to this, I had used an .aspx page to render jpeg images from a binary stream. Basically, this is done by clearing the response content completely in the code-behind of the page and writing the bits to the response content stream. Set the content-type and your good to go.

Well, as it turns out, this technique not only works for images, but *any* binary format that has an associated mime type. A comprehensive list of mime types can be found here. In the case of images, to set the image source to a binary file stored in a database all you do is create a page as I described and then set the src property to the url of that page. So let's say you want to retrieve an image from your database and render it using an img tag and the url of your page is ../mySite/ImageLoader.aspx. You could call the page passing it the id of the record you want to load and render the image like so:
<img src="..mySite/ImageLoader.aspx?imgId=42" />

That's it. Simple.

See where I'm going with this? How is that different from:


<object type="application/x-silverlight-2"
data="data:application/x-silverlight," width="450" height="220">
<param name="source" value="../mySite/ImageLoader.aspx?xapId=42"/>
object>



Answer: It's not. Just have the page set the content type to application/x-silverlight and you are good to go. For reference, here's the code that rewrites the output stream:

int bufferSize = 1024 * 100; // load 100KB at a time
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
long bytesToRead;
MemoryStream ms = new MemoryStream(src.ToArray());

// src is the binary file to read. How you load that is a story for another day

bytesToRead = src.Length;
Response.ContentType = this.GetContentType(filetype);
try
{
while (bytesToRead > 0)
{
if (Response.IsClientConnected)
{
bytesRead = ms.Read(buffer, 0, bufferSize);
Response.OutputStream.Write(buffer, 0, bytesRead);
Response.Flush();

bytesToRead -= bytesRead;

// re-initialize the buffer and counter
bytesRead = 0;
Array.Clear(buffer, 0, buffer.Length);
}
else
{
// make sure to break out of the loop if the client disconnects prematurely
bytesToRead = -1;
}
}
}
catch (Exception ex)
{
string err = ex.Message;
Response.Write("" + ERROR_READING_FILE + "");
}
finally
{
if (ms != null)
{
ms.Close();
}
}