<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Adventures In Development &#187; ASP.NET</title>
	<atom:link href="http://www.adventuresindevelopment.com/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adventuresindevelopment.com</link>
	<description>Web Development Tools, Ideas, Techniques and Resources</description>
	<lastBuildDate>Fri, 27 Aug 2010 04:11:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fast and Efficient C# and Visual Basic String Concatenation</title>
		<link>http://www.adventuresindevelopment.com/2009/06/25/fast-and-efficient-c-and-visual-basic-string-concatenation/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/25/fast-and-efficient-c-and-visual-basic-string-concatenation/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 18:15:12 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=150</guid>
		<description><![CDATA[If you do any sort of web development work on the .NET platform, you are going to find yourself concatenating (connecting) strings together on a very regular basis. There are two ways to do this. The first is with a traditional string concatenation, which would look something like this: string MyString = String.Empty; MyString = [...]]]></description>
			<content:encoded><![CDATA[<p>If you do any sort of web development work on the .NET platform, you are going to find yourself concatenating (connecting) strings together on a very regular basis. There are two ways to do this.</p>
<p><strong>The first is with a traditional string concatenation, which would look something like this:</strong></p>
<p>string MyString = String.Empty;<br />
MyString = &#8220;Hello &#8221; + &#8220;World&#8221;;</p>
<p><strong>The second way, is using the StringBuilder library, which would look something like this:</strong></p>
<p>StringBuilder MyString = new StringBuilder();<br />
MyString.Append(&#8220;Hello &#8220;);<br />
MyString.Append(&#8220;World&#8221;);</p>
<p>These two sets of code do basically the same thing, but is it preferable to use one over the other? It turns out, that the StringBuilder class is much, much more efficient when dealing with large sets of strings. For short strings, with fewer than five concatenations, chances are you&#8217;ll be better off with traditional string concatenation because you don&#8217;t have to instantiate a copy of the StringBuilder library, but for situations that you want to do a lot of string manipulation, you definitely want to use the StringBuilder library.</p>
<p>Here&#8217;s a synthetic test using C# and ASP.NET comparing the two:</p>
<p>        Trace.IsEnabled = true;</p>
<p>        StringBuilder StBuilder = new StringBuilder();<br />
        Trace.Write(&#8220;String Builder Append Begin&#8221;);<br />
        for (int x = 0; x &lt; 10000; x++)<br />
        {<br />
            StBuilder.Append(&#8220;Testing123&#8243;);<br />
        }<br />
        Trace.Write(&#8220;String Builder Complete&#8221;);</p>
<p>        string stString = String.Empty;<br />
        Trace.Write(&#8220;String Concatenation Beginning&#8221;);<br />
        for (int x = 0; x &lt; 10000; x++)<br />
        {<br />
            stString += &#8220;Testing123&#8243;;<br />
        }<br />
        Trace.Write(&#8220;String Concatenation Complete&#8221;);</p>
<p>The test concatenated the string 10,000 times. Although it&#8217;s a synthetic test, it shows that the StringBuilder class is substantially more efficient than the string class for concatenation. The string class concatenated the text in 5.292587 seconds. The StringBuilder class performed the same task in just 0.006142 seconds. In other words, the StringBuilder was over 850 times faster at concatenating the string than the string class was.</p>
<p>Now that&#8217;s a performance benefit!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/25/fast-and-efficient-c-and-visual-basic-string-concatenation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET Performance Tip: Remove Unnecessary Library References</title>
		<link>http://www.adventuresindevelopment.com/2009/06/22/aspnet-performance-tip-remove-unnecessary-library-references/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/22/aspnet-performance-tip-remove-unnecessary-library-references/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 16:12:17 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=148</guid>
		<description><![CDATA[Over the last few days, I&#8217;ve been scouring the web for techniques and strategies to optimize ASP.NET code so that it runs faster and more efficient, resulting in quicker load times. A lot of what I found was pretty standard advice, disable viewstate, use the StringBuilder for concatenation, disable tracing and use AJAX. One piece [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few days, I&#8217;ve been scouring the web for techniques and strategies to optimize ASP.NET code so that it runs faster and more efficient, resulting in quicker load times. A lot of what I found was pretty standard advice, disable viewstate, use the StringBuilder for concatenation, disable tracing and use AJAX. One piece of advice that I didn&#8217;t find when searching was to be selective about the libraries that you reference.</p>
<p><strong>By default, ASP.NET will add the following references to your page:</strong></p>
<p>using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.HtmlControls;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Xml.Linq;<br />
using System.Data.SqlClient;</p>
<p><strong>In a library that I was using, I was able to pair those down to these five libraries:</strong></p>
<p>using System;<br />
using System.Web;<br />
using System.Data;<br />
using System.Data.SqlClient;<br />
using System.Configuration;</p>
<p>After removing the un-necessary libraries, the page load time decreased by nearly half of a second. If you were to do this with all of your libraries and pages, chances are you could see a pretty significant performance increase.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/22/aspnet-performance-tip-remove-unnecessary-library-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Display Your Twitter Feed using ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/06/21/how-to-display-your-twitter-feed-using-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/21/how-to-display-your-twitter-feed-using-aspnet/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 00:08:38 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Social Media]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=145</guid>
		<description><![CDATA[UPDATE 3/23/2010 - Ricky from Twitterizer commented below noting that basic authentication will soon go away via Twitter and OAUTH will be required. Note that the code below will only work for a few months. We will post an updated code-example soon. As I write this article, It&#8217;s about 75 degrees and Sunny outside. When [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 3/23/2010 </strong>- Ricky from Twitterizer commented below noting that basic authentication will soon go away via Twitter and OAUTH will be required. Note that the code below will only work for a few months. We will post an updated code-example soon.</p>
<p>As I write this article, It&#8217;s about 75 degrees and Sunny outside. When I should be going out on a bike ride, instead I&#8217;ve opted to play with <a href="http://code.google.com/p/twitterizer/">Twitterizer</a> (an ASP.NET Twitter Library). Twitterizer is an ASP.NET library that lets you interact with the Twitter API using easy to use objects and methods. It will work with any of the .NET variants (C#, VB, J#, Windows Forms, ASP.NET, WPF, etc). I added the functionality into the <a href="http://www.360webcms.com/">360 Web Content Management System</a> and I thought I&#8217;d share with you how I did it.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to retrieve twitter feeds in ASP.NET</strong></span></p>
<p><strong>(1) Get a copy of the Twitterizer Library </strong></p>
<p>First, you&#8217;ll need to get a copy of the <a href="http://code.google.com/p/twitterizer/">Twitterizer Library from Google&#8217;s Codebase</a>. The download is pretty small and contains only the application library (DLL) you need. Create a new website in ASP.NET and extract the twitterizer library to the /bin/ folder so that you can use it.  Once you have it placed in your /bin/ folder, add a &#8220;using&#8221; reference to the library in the header of your page.</p>
<p>using Twitterizer.Framework;</p>
<p><strong>(2) Create a &#8220;Twitter&#8221; object and Retrieve Your Status Updates.</strong></p>
<p>The library contains a few different objects that you can create. A &#8220;Twitter&#8221; object is the most generic object that you can create. Creating an instance of this object using your username and password gives you all the functionality you would normally have in Twitter, but instead of using the Twitter web interface, you&#8217;re using C# or Visual Basic. First, we&#8217;ll need to instantiate the object, and then get a collection of status updates from your account.</p>
<p>Twitter thisUser = new Twitter(&#8220;UserNameHere&#8221;, &#8220;PasswordHere&#8221;);<br />
TwitterStatusCollection thisCollection = thisUser.Status.UserTimeline();</p>
<p><strong>(3) Loop Through Your Status Updates and Generate Some HTML</strong></p>
<p>The &#8220;TwitterStatusCollection&#8221; object type is a list of &#8220;TwitterStatus&#8221; objects, so you can use a foreach loop and go through your most recent status updates. You&#8217;ll notice in the code below that I also do some basic work with the time of the status update to generate a hyperlink to the page of the status, similar to what Twitter does.</p>
<p>string TwitterCode = &#8220;&#8221;;<br />
foreach (TwitterStatus thisStatus in thisCollection)<br />
{</p>
<p>TimeSpan thisSpan = new TimeSpan();<br />
thisSpan = DateTime.Now.Subtract(thisStatus.Created);</p>
<p>string TimeBetween = &#8220;&#8221;;<br />
if (thisSpan.Days &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; days ago&#8221;; }<br />
else if (thisSpan.Hours &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; hours ago&#8221;;}<br />
else if (thisSpan.Minutes &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; minutes ago&#8221;;}<br />
else if (thisSpan.Seconds &gt; 0) { TimeBetween = thisSpan.Days.ToString() + &#8221; seconds ago&#8221;;}</p>
<p>TwitterCode += &#8220;&lt;div class=&#8217;TwitterStatus&#8217;&gt;&#8221; + thisStatus.Text + &#8221; &lt;a href=&#8217;http://twitter.com/&#8221; + thisStatus.TwitterUser.UserName + &#8220;/status/&#8221; + thisStatus.ID + &#8220;&#8216;&gt;&#8221; + TimeBetween + &#8220;&lt;/a&gt;&lt;/div&gt;&#8221;;<br />
}<br />
<strong>(4) Display Your Tweets </strong></p>
<p>You now have a string with your most recent twitter status updates that you can display on the page using a simple Response.Write() or you can display it in a label. You can see a variation of this code running on the <a href="http://www.360webcms.com/addons/twitter/">&#8220;Twitter&#8221; page for the 360 Web Content Management System</a>.</p>
<p>You can also download a copy of my <a href="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/twitter.zip">sample code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/21/how-to-display-your-twitter-feed-using-aspnet/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>How to Implement an ASP.NET Color Picker</title>
		<link>http://www.adventuresindevelopment.com/2009/06/12/how-to-implement-an-aspnet-color-picker/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/12/how-to-implement-an-aspnet-color-picker/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 20:28:18 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[360 WebCMS]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Content Management Systems]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=139</guid>
		<description><![CDATA[One of the components of the 360 Web Content Management System (website in progress) that I wanted to develop was an events calendar that allowed you to post events into color-coded categories. You can see a demo of it here. At first, I had it so that users would manually enter in a 6-character HTML [...]]]></description>
			<content:encoded><![CDATA[<p>One of the components of the <a href="http://www.360webcms.com/">360 Web Content Management System</a> (website in progress) that I wanted to develop was an events calendar that allowed you to post events into color-coded categories. You can see a demo of it <a href="http://cmsdemo.factor360.com/events.aspx">here</a>. At first, I had it so that users would manually enter in a 6-character HTML color code, but it was very non-intuitive for anyone who&#8217;s never worked with HTML before. Eventually I stumbled upon the <a href="http://www.karpach.com/ColorPickerDemo.aspx">ASP.NET Color Picker control</a>. It&#8217;s a custom ASP.NET control that you can add to a page much in the way that you can add a text box, radio buttons, or a drop down list.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to implement the ASP.NET Color Picker Control</strong></span></p>
<p><strong>(1) Download the library and add it to your project<br />
</strong></p>
<p>First, download the library from the ASP.NET Color Picker Control website. Make sure to download the latest binary release from the website. Currently that version is <a href="http://www.karpach.com/files/WebControls.v.1.4.10423.1-bin.zip">ASP.NET Color Picker v.1.4.10423.1 Binary</a>. Once you get the zip file, it will contain a library that you should extract to the /bin/ folder of your website.</p>
<p><strong>(2) Register the library on your page</strong></p>
<p>ASP.NET provides a set of standard controls that you can add to a page that start with the &#8220;ASP&#8221; prefix, such as &#8220;&lt;ASP:TextBox runat=&#8221;server&#8221; id=&#8221;txtBox&#8221; /&gt;. Any custom controls will have their own prefix that you specify by registering the library on the page. It&#8217;s another line of code that you add to the top of the page next to your page definition. It should look something like this:</p>
<p>&lt;%@ Register Assembly=&#8221;Karpach.WebControls&#8221; Namespace=&#8221;Karpach.WebControls&#8221; TagPrefix=&#8221;cc1&#8243; %&gt;</p>
<p><strong>(3) Add the control to your page</strong></p>
<p>Now that you have the library referenced, you can add the control to your page and make use of it.  For the purpose of this demo, I&#8217;m going to set the AutoPostBack property to true and run a function whenever the color is changed. This will show us the color that we picked inside of a label (also shown below) after we select a new color.</p>
<p>&lt;cc1:ColorPicker ID=&#8221;colorBackgroundColor&#8221; runat=&#8221;server&#8221; AutoPostBack=&#8221;true&#8221; OnColorChanged=&#8221;chngColor&#8221; /&gt;<br />
&lt;br /&gt;&lt;br /&gt;<br />
&lt;asp:Label ID=&#8221;lblResults&#8221; runat=&#8221;server&#8221; Text=&#8221;"&gt;&lt;/asp:Label&gt;</p>
<p><strong>(4) Create Your C# Function</strong></p>
<p>After we choose a color, we have to do something with it. With the ColorPicker control above, I&#8217;m using the OnColorChanged property to call the &#8220;chngColor&#8221; function, which in C# will look something like this. This will also demonstrate how to programmatically read the color chosen with the .Color property of the ASP.NET Color Picker Control</p>
<p>protected void chngColor(object sender, EventArgs e)<br />
{<br />
lblResults.Text = &#8220;&lt;div style=&#8217;background-color:#&#8221; + colorBackgroundColor.Color.Replace(&#8220;#&#8221;, &#8220;&#8221;) + &#8220;;height:50px;width:80px;text-align:center;padding-top:35px;&#8217;&gt;Sample Text&lt;/div&gt;&#8221;;<br />
}</p>
<p><strong>(5) Success</strong></p>
<p>So far, we&#8217;ve added the library to our project, registered the library on the page, added the control to the page, and done something with the color chosen by the user. Your page should look something like this:</p>
<p><a href="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/color-picker.jpg"><img class="alignnone size-full wp-image-140" title="color-picker" src="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/color-picker.jpg" alt="color-picker" width="567" height="398" /></a></p>
<p><a href="http://www.adventuresindevelopment.com/wp-content/uploads/2009/06/colorpickerdemo.zip">You can download my sample program here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/12/how-to-implement-an-aspnet-color-picker/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Validate Email Addresses in C#</title>
		<link>http://www.adventuresindevelopment.com/2009/06/08/how-to-validate-email-addresses-in-c/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/08/how-to-validate-email-addresses-in-c/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 14:11:48 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=137</guid>
		<description><![CDATA[I was recently doing doing support for a client that had a newsletter system. The previous employee had neglected to do much in the form of format validation for email addresses from both a user-input standpoint and system-integrity standpoint. Since there were several email addresses in the database that didn&#8217;t meet the basic conventions of [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently doing doing support for a client that had a newsletter system. The previous employee had neglected to do much in the form of format validation for email addresses from both a user-input standpoint and system-integrity standpoint. Since there were several email addresses in the database that didn&#8217;t meet the basic conventions of an email address, the user received the following error whenever she tried to send out a message:</p>
<p><strong>Exception Details: </strong>System.FormatException: The specified string is not in the form required for an e-mail address.</p>
<p>Ouch. To remedy this issue, I added a check to make sure the email address was valid before it attempted to send the message. In the code below, I&#8217;m making use of the System.Text.RegularExpressions library that comes with the .NET framework. The code below is written in C# but the code will be very similar in Visual Basic. It will also work in ASP.NET, WPF or plain old windows forms.</p>
<p><strong>Here&#8217;s a C# function that will determine whether or not an email address is valid:</strong><br />
<code><br />
public static bool IsValidEmail(string strEmailAddress)<br />
{<br />
if (strEmailAddress == null)<br />
{<br />
return false;<br />
}<br />
else<br />
{<br />
return System.Text.RegularExpressions.Regex.IsMatch(strEmailAddress, @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|[a-zA-Z]{2})$", RegexOptions.IgnorePatternWhitespace);<br />
}<br />
}<br />
</code></p>
<p>I also made some modifications to the system on the front-end, so when a user registered from then on, that it would validate that they have entered an email address and that the email address matched the format of an email address using a RequiredFieldValidator and a RegularExpressionValidator.&lt;&#8211;&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/08/how-to-validate-email-addresses-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hash Passwords in C# and Visual Basic Using SHA-512</title>
		<link>http://www.adventuresindevelopment.com/2009/06/02/hash-passwords-in-c-and-visual-basic-using-sha-512/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/02/hash-passwords-in-c-and-visual-basic-using-sha-512/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 20:26:31 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=126</guid>
		<description><![CDATA[We recently covered an easy way to hash passwords using SHA-1 in .NET using either Visual Basic or C#. In most cases, SHA-1 encryption is &#8220;secure enough&#8221;, but there are some mathematical weaknesses. Microsoft&#8217;s .NET platform (specifically the System.Security class) allows you to encrypt passwords with a number of differnet algorithms without having to know the [...]]]></description>
			<content:encoded><![CDATA[<p>We recently covered an <a href="http://www.adventuresindevelopment.com/2009/05/23/a-simple-way-to-hash-passwords-in-aspnet/">easy way to hash passwords using SHA-1</a> in .NET using either Visual Basic or C#. In most cases, SHA-1 encryption is &#8220;secure enough&#8221;, but there are some <a href="http://www.schneier.com/blog/archives/2005/02/cryptanalysis_o.html">mathematical weaknesses</a>. Microsoft&#8217;s .NET platform (specifically the System.Security class) allows you to encrypt passwords with a number of differnet algorithms without having to know the mathematics behind them.</p>
<p>Today, we&#8217;re going to encrypt a string with SHA-2, specifically the SHA-512 derivation of SHA-2, which should hypothetically be more secure than SHA-1 because it has a longer message digest than SHA-1. The example code I&#8217;m going to show off today also uses a &#8220;<a href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salt</a>&#8220;, whereas the previous function I showed off didn&#8217;t. This will make your hashed-passwords more immume to dictionary attacts because not only would the hacker have to develop a hash for every commonly known password, but as well as every commonly known password multiplied by the nearly infinite number of possible salts.</p>
<p><strong>Here&#8217;s the function:</strong></p>
<p>    public static string CreateSHAHash(string Password, string Salt)<br />
    {<br />
        System.Security.Cryptography.SHA512Managed HashTool = new System.Security.Cryptography.SHA512Managed();<br />
        Byte[] PasswordAsByte = System.Text.Encoding.UTF8.GetBytes(string.Concat(Password, Salt));<br />
        Byte[] EncryptedBytes = HashTool.ComputeHash(PasswordAsByte);<br />
        HashTool.Clear();<br />
        return Convert.ToBase64String(EncryptedBytes);<br />
    }</p>
<p><strong>How it works:</strong></p>
<p>This method makes use of the System.Security.Cryptography class. It combines your password and the salt that you provide and  turns it into a byte-array. It runs those bytes through the has computation function provided by the class and returns an 88-bit string of the message-digest/hash that&#8217;s created.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/02/hash-passwords-in-c-and-visual-basic-using-sha-512/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Authenticate a User in Active Directory using ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-using-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-using-aspnet/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 15:58:41 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=123</guid>
		<description><![CDATA[If you&#8217;re working in an academic or large corporate or government setting, changes are you&#8217;re going to have a network in place using Active Directory or an open-source equivalent. Every user in the organization will have some sort of an account to use. If you&#8217;re building an internal web-application or desktop-application, it doesn&#8217;t make a [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working in an academic or large corporate or government setting, changes are you&#8217;re going to have a network in place using Active Directory or an open-source equivalent. Every user in the organization will have some sort of an account to use. If you&#8217;re building an internal web-application or desktop-application, it doesn&#8217;t make a lot of sense to give the user another set of credentials. Instead, you can validate users by checking the permissions existing Active Directory accounts.</p>
<p>The source code to check a user&#8217;s credentials in Active Directory using C# or Visual Basic is actually fairly minimal. This works with both ASP.NET and with Windows Forms  (or WPF for that matter) if you&#8217;re building a desktop application.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to do it:</strong></span></p>
<p><strong>(1) Reference the appropriate library</strong></p>
<p>You&#8217;ll need to make use of the System.DirectoryServices library that comes with Visual Studio. You can add this to your ASP.NET code-behind page or your C# class for your Windows forms like this.</p>
<p><em>using System.DirectoryServices;</em></p>
<p><strong>(2) Create</strong><em> <strong>An Authentication Function.</strong></em></p>
<p>Here&#8217;s a basic function that will check a user&#8217;s permissions on a given domain. Essentially, it will try to create an Active Directory entry using the provided credentials, and it can successfully create a valid entry, we know that the user is authenticated. Otherwise, it&#8217;ll return false.</p>
<p>public bool AuthenticateActiveDirectory(string Domain, string UserName, string Password)<br />
{<br />
try<br />
{<br />
DirectoryEntry entry = new DirectoryEntry(&#8220;LDAP://&#8221; + Domain, UserName, Password);<br />
object nativeObject = entry.NativeObject;<br />
return true;<br />
}<br />
catch (DirectoryServicesCOMException) { return false; }<br />
}</p>
<p>That&#8217;s really all there is to it. Microsoft has an <a href="http://msdn.microsoft.com/en-us/library/ms180890(VS.80).aspx" target="_blank">extensive aritcle</a> on MSDN that covers active directory authentication in .NET that you might want to check out as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-using-aspnet/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Resize Photos in ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/06/01/how-to-resize-photos-in-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/06/01/how-to-resize-photos-in-aspnet/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 18:28:23 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=121</guid>
		<description><![CDATA[If you&#8217;re developing a website that allows users to upload any sort of photos or images, you have to expect that they aren&#8217;t going to bother doing any sort of basic image manipulation, such as resizing an 8 megapixel image down to something that&#8217;s appropriate for the web. Fortunately, there are a number of good [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re developing a website that allows users to upload any sort of photos or images, you have to expect that they aren&#8217;t going to bother doing any sort of basic image manipulation, such as resizing an 8 megapixel image down to something that&#8217;s appropriate for the web. Fortunately, there are a number of good cropping and image resizing tools that are available for most server-side programming languages.</p>
<p>ASP.NET does a particularly good job of offering the ability to resize photos. The System.Drawing library that comes with .NET allows you to resize photos extremely well with just a few lines of code, and place it in the format that you&#8217;d like.</p>
<p><span style="text-decoration: underline;"><strong>Here&#8217;s how to resize photos in the .NET Framework:</strong></span></p>
<p><strong>(1) Reference the appropriate libraries.</strong></p>
<p>The example code below will be in C#, but it will work basically the same way for Visual Basic or J#. You&#8217;ll need to reference the System.IO library to read and write the files, as well as the System.Drawing library and the System.Drawing.Imaging library. To do this, simply add the following lines to the top of your code-behind page.</p>
<p>using System.IO;<br />
using System.Drawing;<br />
using System.Drawing.Imaging;</p>
<p><strong>(2) Load the OriginalImage</strong></p>
<p>The following three lines of code will take an existing image that you can set programmatically, say if you want to connect it to a photo gallery module in a database, and create a bitmap/image object that you can use to resize the image:</p>
<p>string UploadPath = Server.MapPath(&#8220;/images/&#8221;);<br />
string FileName = &#8220;OriginalImage.PNG&#8221;;<br />
Bitmap OriginalBM = new Bitmap(UploadPath + FileName);</p>
<p><strong>(3) Determine The New Size</strong></p>
<p>Typically you&#8217;ll want to keep the aspect ratio the same, but you can set them statically if you&#8217;d like.  Here&#8217;s some code that will create two variables for the height and the width. We set the width statically based on what we&#8217;d like to use, then the height is based on the aspect ratio of the original image so that the image is not stretched.Finally, we&#8217;ll create a &#8220;Size&#8221; object that will be used for the resizing</p>
<p>float AspectRatio = (float)OriginalBM.Size.Width / (float)OriginalBM.Size.Height;<br />
int NewWidth = 800;<br />
int newHeight = NewWidth / AspectRatio;<br />
Size newSize = new Size(NewWidth, newHeight);<strong><br />
</strong></p>
<p><strong>(4) Resize and Save the Photo</strong></p>
<p>Once you have a new &#8220;Size&#8221; object created, we can create a new Bitmap/image object with the new size that we specify and save it in the format of image that we&#8217;d like. In this case, I&#8217;m saving it as a Jpeg file, but you can also save it as a PNG, GIF, or Bitmap file if you&#8217;d like.</p>
<p>Bitmap ResizedBM = new Bitmap(OriginalBM, newSize);<br />
string newfilepath = UploadPath + &#8220;resized_&#8221; + FileName.Replace(@&#8221;\&#8221;, @&#8221;");<br />
ResizedBM.Save(newfilepath, ImageFormat.Jpeg);</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/06/01/how-to-resize-photos-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple Way to Hash Passwords in ASP.NET</title>
		<link>http://www.adventuresindevelopment.com/2009/05/23/a-simple-way-to-hash-passwords-in-aspnet/</link>
		<comments>http://www.adventuresindevelopment.com/2009/05/23/a-simple-way-to-hash-passwords-in-aspnet/#comments</comments>
		<pubDate>Sat, 23 May 2009 15:17:35 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=83</guid>
		<description><![CDATA[If you&#8217;re developing a website that requires your users to create an account, it&#8217;s a very good idea to not store their passwords in plain-text in the database. A good chunk of users use the same password for just about everything, so if your database is compromised, there&#8217;s the possibility of having some real reprocussions [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re developing a website that requires your users to create an account, it&#8217;s a very good idea to not store their passwords in plain-text in the database. A good chunk of users use the same password for just about everything, so if your database is compromised, there&#8217;s the possibility of having some real reprocussions for your users. Fortunately, it&#8217;s very easy to hash passwords in ASP.NET (and C# and Visual Basic in general).</p>
<p>Microsoft has provided us with a method called <a href="http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.hashpasswordforstoringinconfigfile.aspx">FormsAuthentication.HashPasswordForStoringInConfigFile</a> that will hash your user&#8217;s passwords with <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> or <a href="http://en.wikipedia.org/wiki/SHA_hash_functions">SHA-1</a> with a single line of code. Given the choice, I&#8217;d recommend SHA-1 because it&#8217;s generally considered more secure by the security community.</p>
<p><strong>So what&#8217;s password hashing anyway?</strong></p>
<p>Essentially, a hash function provides a means to take a string of text that you want to protect and encrypts it in such a manner that if the original text were ran through the function again, it would always generate the same result. Hash functions are generally a &#8220;one-way&#8221; encryption, so you can take the original password and turn it into the hashed password, but you can&#8217;t go back from the hashed password and turn it back into the original.</p>
<p>If you&#8217;d like a more technical explanation of password hashing, checkout this article on <a href="http://mathworld.wolfram.com/HashFunction.html">MathWorld</a>.</p>
<p><strong>Using the Function:</strong></p>
<p>public static string PasswordHasher(string Password)<br />
{<br />
return FormsAuthentication.HashPasswordForStoringInConfigFile(Password, System.Web.Configuration.FormsAuthPasswordFormat.SHA1);<br />
}</p>
<p>The above method will take a password that you enter and run it through the encryption function provided using the SHA1 format. You&#8217;ll get back a string with the generated hash of your password.</p>
<p>Overall, it&#8217;s a very nice quick and dirty way to hash a password in ASP.NET, <a href="http://blog.veggerby.dk/2008/07/06/abuse-of-formsauthenticationhashpasswordforstoringinconfigfile-method/">although some might criticize its use</a>. If you would like to at a SALT to your password, <a href="http://www.aspnextgen.com/Tutorial/77D4AFDC-585D-4539-A364-30028327FF14.dcik">read this article at donetjunkies</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/05/23/a-simple-way-to-hash-passwords-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASP.NET Caching Techniques and Tools</title>
		<link>http://www.adventuresindevelopment.com/2009/05/21/aspnet-caching-techniques-and-tools/</link>
		<comments>http://www.adventuresindevelopment.com/2009/05/21/aspnet-caching-techniques-and-tools/#comments</comments>
		<pubDate>Thu, 21 May 2009 20:36:19 +0000</pubDate>
		<dc:creator>Matthew Paulson</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Database]]></category>

		<guid isPermaLink="false">http://www.adventuresindevelopment.com/?p=78</guid>
		<description><![CDATA[One of the most frequent criticisms of the ASP.NET stack is that it&#8217;s less efficient than some of its open-source counterparts. However, Microsoft has actually built in a lot of neat technology into the framework related to caching that allows for much more scalability than you might think. Not only is there caching support built [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most frequent criticisms of the ASP.NET stack is that it&#8217;s less efficient than some of its open-source counterparts. However, Microsoft has actually built in a lot of neat technology into the framework related to caching that allows for much more scalability than you might think. Not only is there caching support built directly into ASP.NET, but there are actually 3 different ways to cache an ASP.NET page and related data.</p>
<p><strong><span style="text-decoration: underline;">The Output Cache</span></strong></p>
<p>Using the output cache is probably the easiest implementation of caching in ASP.NET because all you need to do is add a line to your header. This will essentially cache the entire page. This way, when you load the page a second time, it will serve up the generated HTML of the cached page rather than by going through the page lifecycle and reloading the page.</p>
<p>Here&#8217;s the line of code you need to add  to the top of your .ASPX pages to implement the Output Cache:</p>
<p><code><strong>&lt;%@ OutputCache Duration="60" VaryByParam="*" %&gt;</strong></code></p>
<p>The &#8220;Duration&#8221; property indicates the number of seconds that the cache is good for. The &#8220;VaryByParam&#8221; property allows you to create separate cached versions of the page if it&#8217;s given different querystrings. For example, if you had a content management system where your Page&#8217;s looked like http://www.example.org/Default.aspx?PageID=85, you wouldn&#8217;t want that to show up the same as http://www.example.org/Default.aspx?PageID=92. They&#8217;re separate dynamic pages on the site that&#8217;s generated from the page. You don&#8217;t want to cache one sub-page then serve that cache for the entire site.</p>
<p><strong><span style="text-decoration: underline;">The Data Cache</span></strong></p>
<p>If you need to cache any sort of information in ASP.NET, such as a DataSet, a DataTable, or even a variable, you can use the ASP.NET DataCache. This works a lot like a session or application variable, but it has some added cache-related methods as well.</p>
<p><strong>Here&#8217;s how to store something in the cache in C#:</strong></p>
<p><code>DataTable dtToBeCached = new DataTable();<br />
Cache["CachedTable"] = dtToBeCached;<br />
</code><br />
<strong>Here&#8217;s how to turn that cached object back into a DataTable:</strong></p>
<p><code>DataTable dtToBeCached = new DataTable();<br />
if(Cache["CachedTable"] != null)<br />
{<br />
dtToBeCached = (DataTable)Cache["CachedTable"];<br />
}<br />
</code><br />
If you would like to remove something from the cache at a later time, you can simply use the Cache.Remove() method.</p>
<p><strong><span style="text-decoration: underline;">Partial Page Caching:</span></strong></p>
<p>There are some cases, such as when having a website that allows for users to login, that you would want to cache some parts of page but not the entireity. Let&#8217;s say that you cached a page where one user was authenticated and that paged was cached. Then, the authenticated version of that page would be served to everyone, including individuals that aren&#8217;t logged in. In that case, you don&#8217;t want to use the Output cache onthe entire page. </p>
<p>Instead, you want to make use of the DataCache to store shared data that would be frequently read from the database. The other strategy you can use in this case is to cache ASP.NET user controls that you develop. You cache these much in the same way that you would cache a page on a website, but instead you would add that code to the user control&#8217;s header instead of the page&#8217;s header.</p>
<p><strong><span style="text-decoration: underline;">You might also want to read these articles related to ASP.NET Caching:</span></strong></p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/aa478965.aspx">ASP.NET Caching: Techniques and Best Practices</a> (MSDN)</li>
<li><a href="http://www.devcity.net/Articles/209/1/article.aspx">Advanced Caching Techniques in ASP.NET 2.0</a> (DevCity)</li>
<li><a href="http://www.beansoftware.com/ASP.NET-Tutorials/Caching-Techniques.aspx">Caching Techniques in ASP.NET 2.0</a> (Bean Software).</li>
<li><a href="http://www.4guysfromrolla.com/articles/022802-1.aspx">Caching with ASP.NET </a>(4 Guys From Rolla)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.adventuresindevelopment.com/2009/05/21/aspnet-caching-techniques-and-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
