<?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>Adnan Rashid &#187; c#</title>
	<atom:link href="http://www.adnan-rashid.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adnan-rashid.com</link>
	<description>Web Technologist</description>
	<lastBuildDate>Wed, 23 Jun 2010 20:43:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>NOLOCK in LINQ</title>
		<link>http://www.adnan-rashid.com/2010/05/08/nolock-in-linq/</link>
		<comments>http://www.adnan-rashid.com/2010/05/08/nolock-in-linq/#comments</comments>
		<pubDate>Sat, 08 May 2010 04:00:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=55</guid>
		<description><![CDATA[The NOLOCK table hint, also known as READUNCOMITTED, is applicable to SELECT statements only. NOLOCK indicates that no shared locks are issued against the table that would prohibit other transactions from modifying the data in the table. The most recommended way of implementing NOLOCK is to use a TransactionScope to affect the TransactionOptions associated with [...]]]></description>
			<content:encoded><![CDATA[<p>The NOLOCK table hint, also known as READUNCOMITTED, is applicable to SELECT statements only. NOLOCK indicates that no shared locks are issued against the table that would prohibit other transactions from modifying the data in the table.</p>
<p>The most recommended way of implementing NOLOCK is to use a TransactionScope to affect the TransactionOptions associated with the commands of the LINQ context.</p>
<p>Heres a sample code :</p>
<pre name="code" class="csharp">

using (new System.Transactions.TransactionScope(
	System.Transactions.TransactionScopeOption.Required,
	new System.Transactions.TransactionOptions
	{
		IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
	}))
{
	// Retreive Data
}
</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2010/05/08/nolock-in-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Row &#8211; Linq to SQL</title>
		<link>http://www.adnan-rashid.com/2010/04/04/random-row-linq-to-sql/</link>
		<comments>http://www.adnan-rashid.com/2010/04/04/random-row-linq-to-sql/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 04:00:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=53</guid>
		<description><![CDATA[You can retrieve a random data record from an SQL database through LINQ using a User Defined Function. We can implement the above in a partial class for the target context. partial class MyTargetContext { [Function(Name="NEWID", IsComposable=true)] public Guid Random() { throw new NotImplementedException(); } } In the above example which returns a uniqe identifier [...]]]></description>
			<content:encoded><![CDATA[<p>You can retrieve a random data record from an SQL database through LINQ using a User Defined Function. We can implement the above in a partial class for the target context.</p>
<pre name="code" class="csharp">
partial class MyTargetContext {
     [Function(Name="NEWID", IsComposable=true)]
     public Guid Random()
     {
         throw new NotImplementedException();
     }
}
</pre>
<p>In the above example which returns a uniqe identifier (GUID), we are mapping the Random method to the NEWID SQL function. The IsComposable boolean property of the Function Attribute, defines if the method is mapped to a function (true) or to a Stored Procedure (false). Also note that since this method is mapped, the C# code is not executed.</p>
<p>We can call the above implementation as &#8230;</p>
<pre name="code" class="csharp">
var posts = (from post in dbContext.Posts
			where post.IsActive &#038;&#038; post.Category.Equals('Linq')
			order by dbContext.Random()
			select post).FirstOrDefault();
</pre>
<p>Although there are better alternative in terms of performance, this approach is much easier and should work fine for small / mid-size tables.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2010/04/04/random-row-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ObjectTrackingEnabled &#8211; LINQ</title>
		<link>http://www.adnan-rashid.com/2010/02/18/objecttrackingenabled-linq/</link>
		<comments>http://www.adnan-rashid.com/2010/02/18/objecttrackingenabled-linq/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 04:00:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=51</guid>
		<description><![CDATA[You may come across a requirement, where you will retrieve data only for reading. LINQ monitors the changes, which as you might have guessed is the not the best solution everywhere, especially in the above scenario. Here is a quick tip to increase the performance by disabling object tracking in LINQ using the ObjectTrackingEnabled property. [...]]]></description>
			<content:encoded><![CDATA[<p>You may come across a requirement, where you will retrieve data only for reading. LINQ monitors the changes, which as you might have guessed is the not the best solution everywhere, especially in the above scenario.</p>
<p>Here is a quick tip to increase the performance by disabling object tracking in LINQ using the ObjectTrackingEnabled property. This will turn off the unnecessary identity management of the objects.</p>
<pre name="code" class="csharp">
using (NorthwindDataContext dbContext = new NorthwindDataContext())
{
	dbContext.ObjectTrackingEnabled = false;
}
</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2010/02/18/objecttrackingenabled-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>vCalendar &#8211; C# Implementation</title>
		<link>http://www.adnan-rashid.com/2008/11/18/vcalendar-c-implementation/</link>
		<comments>http://www.adnan-rashid.com/2008/11/18/vcalendar-c-implementation/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 04:00:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=114</guid>
		<description><![CDATA[I needed to integrate support for vCalendar in my current project. So i went through the vCalendar spec 1.0 at http://www.imc.org/pdi/vcal-10.txt and wrote this class. Although I have not implemented all the specification features, but this should prove sufficient for the usual requirements. If you have any comments / suggestions , let me know. using [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to integrate support for vCalendar in my current project. So i went through the vCalendar spec 1.0 at <a title="vCalendar 1.0" href="http://www.imc.org/pdi/vcal-10.txt" target="_blank">http://www.imc.org/pdi/vcal-10.txt</a> and wrote this class. Although I have not implemented all the specification features, but this should prove sufficient for the usual requirements. If you have any comments / suggestions , let me know.</p>
<pre name="code" class="csharp">
using System;
using System.Text;
using System.IO;
using System.Collections;

/// &lt;summary&gt;
/// Summary description for VCalendar
/// &lt;/summary&gt;
public class VCalendar
{
	private string _productID;
	/// &lt;summary&gt;
	/// A unique identifier identifying the originating program
	/// &lt;/summary&gt;
	public string ProductIdentifier
	{
		get { return _productID; }
		set { _productID = value; }
	}

	private ArrayList _categories;
	/// &lt;summary&gt;
	/// ArrayList of categories
	/// &lt;/summary&gt;
	public ArrayList Categories
	{
		get { return _categories; }
		set { _categories = value; }
	}

	private DateTime _createdOn = DateTime.MinValue;
	/// &lt;summary&gt;
	/// DateTime that the vEvent was created.
	/// &lt;/summary&gt;
	public DateTime CreatedOn
	{
		get { return _createdOn; }
		set { _createdOn = value; }
	}

	private DateTime _completedOn = DateTime.MinValue;
	/// &lt;summary&gt;
	/// DateTime the vEvent completed on
	/// &lt;/summary&gt;
	public DateTime CompletedOn
	{
		get { return _completedOn; }
		set { _completedOn = value; }
	}

	private DateTime _startOn = DateTime.MinValue;
	/// &lt;summary&gt;
	/// DateTime that the vEvent will start
	/// &lt;/summary&gt;
	public DateTime StartOn
	{
		get { return _startOn; }
		set { _startOn = value; }
	}

	private DateTime _endOn = DateTime.MinValue;
	/// &lt;summary&gt;
	/// DateTime that the vEvent will end
	/// &lt;/summary&gt;
	public DateTime EndOn
	{
		get { return _endOn; }
		set { _endOn = value; }
	}

	private ClassificationType _classification = ClassificationType.PUBLIC;
	/// &lt;summary&gt;
	/// Defines the access classification for the vEvent entity
	/// &lt;/summary&gt;
	public ClassificationType Classification
	{
		get { return _classification; }
		set { _classification = value; }
	}

	private int _priority = 0;
	/// &lt;summary&gt;
	/// 0 = undefined, 1 = High, -1 = Low. Defaults to 0
	/// &lt;/summary&gt;
	public int Priority
	{
		get { return _priority; }
		set { _priority = value; }
	}

	private string _subject;
	/// &lt;summary&gt;
	/// Subject of the vEvent
	/// &lt;/summary&gt;
	public string Subject
	{
		get { return _subject; }
		set { _subject = value; }
	}

	private string _description;
	/// &lt;summary&gt;
	/// Description of the vEvent
	/// &lt;/summary&gt;
	public string Description
	{
		get { return _description; }
		set { _description = value; }
	}

	private bool _enableAlarm;
	/// &lt;summary&gt;
	/// Flag to enable the reminder alarm
	/// &lt;/summary&gt;
	public bool EnableAlarm
	{
		get { return _enableAlarm; }
		set { _enableAlarm = value; }
	}

	public override string ToString()
	{
		StringBuilder sb = new StringBuilder();
		sb.Append(“BEGIN:VCALENDAR\n”);
		sb.Append(“VERSION:1.0\n”);
		sb.Append(“BEGIN:VEVENT\n”);

		// Product Identifier
		if (!string.IsNullOrEmpty(_productID))
			sb.AppendFormat(“PRODID:{0}\n”, _productID);

		// Categories
		if (_categories != null &amp;&amp; _categories.Count != 0)
		{
			sb.Append(“CATEGORIES:”);
			for (int i = 0; i &lt; _categories.Count; i++)
			{
				if (i == 0)
					sb.Append(_categories[0].ToString());
				else
					sb.AppendFormat(“;{0}”, _categories[i].ToString());
			}
			sb.Append(“\n”);
		}

		// Created On
		if (_createdOn != DateTime.MinValue)
			sb.AppendFormat(“DCREATED:{0}\n”, _createdOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

		// Start Date Time
		if (_startOn != DateTime.MinValue)
		{
			sb.AppendFormat(“DTSTART:{0}\n”, _startOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

			// Display Reminder
			if (_enableAlarm)
				sb.AppendFormat(“DALARM:{0};PT5M;2;{1}\n”, _startOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”), _subject);
		}

		// End Date Time
		if (_endOn != DateTime.MinValue)
			sb.AppendFormat(“DTEND:{0}\n”, _endOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

		// Completed On
		if (_completedOn != DateTime.MinValue)
			sb.AppendFormat(“COMPLETED:{0}\n”, _completedOn.ToUniversalTime().ToString(“yyyyMMddTHHmmssZ”));

		// Priority
		if (_priority != 0)
			sb.AppendFormat(“PRIORITY:{0}\n”, _priority);

		// Description
		if (!string.IsNullOrEmpty(_description))
			sb.AppendFormat(“DESCRIPTION:{0}\n”, _description);

		// Subject
		if (!string.IsNullOrEmpty(_subject))
			sb.AppendFormat(“SUMMARY:{0}\n”, _subject);
		if (_classification != ClassificationType.PUBLIC)
			sb.AppendFormat(“CLASS:{0}\n”, Enum.GetName(typeof(ClassificationType), Classification));
		sb.Append(“END:VEVENT\n”);
		sb.Append(“END:vCalendar”);
		return sb.ToString();
	}

	public void Generate(string filePath, FileMode mode)
	{
		FileStream fs = new FileStream(filePath, mode);
		StreamWriter sw = new StreamWriter(fs);
		using (sw)
		{
			sw.Write(this.ToString());
		}
	}
}

public class Attendee
{
	private StatusType _status;
	public StatusType Status
	{
		get { return _status; }
		set { _status = value; }
	}

	private AttendeeType _role;
	public AttendeeType Role
	{
		get { return _role; }
		set { _role = value; }
	}

	public string _attendeeName;
	public string AttendeeName
	{
		get { return _attendeeName; }
		set { _attendeeName = value; }
	}

	public override string ToString()
	{
		StringBuilder sb = new StringBuilder();
		if (!string.IsNullOrEmpty(_attendeeName))
		{
			sb.Append(“ATTENDEE”);
			if (_role != AttendeeType.ATTENDEE)
				sb.AppendFormat(“;ROLE={0}”, Enum.GetName(typeof(AttendeeType), Role));
			if (_status != StatusType.NEEDS_ACTION)
				sb.AppendFormat(“;STATUS={0}”, Enum.GetName(typeof(StatusType), Status));
			sb.AppendFormat(“:{0}\n”, _attendeeName);
			return sb.ToString();
		}
		else
			return string.Empty;
	}
}
public enum AttendeeType
{
	ATTENDEE,
	ORGANIZER,
	OWNER,
	DELEGATE,
}
public enum StatusType
{
	ACCEPTED,
	NEEDS_ACTION,
	SENT,
	TENTATIVE,
	CONFIRMED,
	DECLINED,
	COMPLETED,
	DELEGATED
}
public enum RSVPType
{
	YES,
	NO
}
public enum ExpectType
{
	FYI,
	REQUIRE,
	REQUEST,
	IMMEDIATE
}
public enum CategoryType
{
	APPOINTMENT,
	BUSINESS,
	EDUCATION,
	HOLIDAY,
	MEETING,
	MISCELLANEOUS,
	PERSONAL,
	TRAVEL,
	VACATION
}
public enum ClassificationType
{
	PUBLIC,
	PRIVATE,
	CONFIDENTIAL
}
</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2008/11/18/vcalendar-c-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An anonymous type cannot have multiple properties with the same name</title>
		<link>http://www.adnan-rashid.com/2008/10/10/an-anonymous-type-cannot-have-multiple-properties-with-the-same-name/</link>
		<comments>http://www.adnan-rashid.com/2008/10/10/an-anonymous-type-cannot-have-multiple-properties-with-the-same-name/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 04:00:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[linq]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=116</guid>
		<description><![CDATA[If you are using LINQ and have a query that looks something like this : var myUser = (from user in dbContext.Users join comp in dbContext.Companies on user.CompanyID equals comp.CompanyID where user.UserID == "12345" select new {user.UserID, user.Name, comp.Name}).SingleOrDefault(); string _userID = myUser.UserID; string _userName = myUser.Name; string _companyName = myUser.Name; In the above code, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using LINQ and have a query that looks something like this :</p>
<pre name="code" class="csharp">
var myUser = (from user in dbContext.Users
				join comp in dbContext.Companies on user.CompanyID equals comp.CompanyID
				where user.UserID == "12345"
				select new {user.UserID, user.Name, comp.Name}).SingleOrDefault();

string _userID = myUser.UserID;
string _userName = myUser.Name;
string _companyName = myUser.Name;
</pre>
<p>In the above code, example we are using a syntactic sugar feature of C# 3.0 by building anonymous types. When you try to compile the above code, you will get an <strong>&#8220;An anonymous type cannot have multiple properties with the same name&#8221;</strong> error. This is because the compiler can&#8217;t differentiate if the <strong>Name</strong> property belongs to the Users table or the Companies table. So to solve this problem, we need to assign something like an alias to the conflicting properties.</p>
<pre name="code" class="csharp">
var myUser = (from user in dbContext.Users
				join comp in dbContext.Companies on user.CompanyID equals comp.CompanyID
				where user.UserID == "12345"
				select new {user.UserID, UserName = user.Name, CompanyName = comp.Name}).SingleOrDefault();

string _userID = myUser.UserID;
string _userName = myUser.UserName;
string _companyName = myUser.CompanyName;
</pre>
<p>Happy coding!!!</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2008/10/10/an-anonymous-type-cannot-have-multiple-properties-with-the-same-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asp.Net Read Reciept Request</title>
		<link>http://www.adnan-rashid.com/2008/09/28/asp-net-read-reciept-request/</link>
		<comments>http://www.adnan-rashid.com/2008/09/28/asp-net-read-reciept-request/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 04:00:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=118</guid>
		<description><![CDATA[Here&#8217;s a quick tip. If you need to include a request for read receipt like Microsoft Outlook while sending mails through Asp.net, just add the Disposition-Notification-To Header to the MailMessage. The MailMessage class does not have a corresponding attribute and thus it has to be set through the Header. MailMessage mm = new MailMessage(fromAddress, toAddress); [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip. If you need to include a request for read receipt like Microsoft Outlook while sending mails through Asp.net, just add the <strong>Disposition-Notification-To</strong> Header to the MailMessage.</p>
<p>The MailMessage class does not have a corresponding attribute and thus it has to be set through the Header.</p>
<pre class="csharp">MailMessage mm = new MailMessage(fromAddress, toAddress);
mm.Subject = subject;
mm.Headers.Add("Disposition-Notification-To", sendReadReceiptToAddress);
</pre>
<p>Please keep in mind that this is only a request, and can be rejected/ignored by the Mail Client/User configuration.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2008/09/28/asp-net-read-reciept-request/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic Handler &#8211; Session State</title>
		<link>http://www.adnan-rashid.com/2008/09/26/generic-handler-session-state/</link>
		<comments>http://www.adnan-rashid.com/2008/09/26/generic-handler-session-state/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 04:00:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=120</guid>
		<description><![CDATA[In my current project I needed to access the Session state in a Generic Handler. To my dismay, i realized that the Session StateBag was null. The StateBag class is sealed and manages the Viewstate of the Asp.net server controls and pages. Upon discussion with my colleague, Mr. Hamed, we found the solution to my [...]]]></description>
			<content:encoded><![CDATA[<p>In my current project I needed to access the Session state in a Generic Handler. To my dismay, i realized that the Session StateBag was null. The StateBag class is sealed and manages the Viewstate of the Asp.net server controls and pages.</p>
<p>Upon discussion with my colleague, Mr. Hamed, we found the solution to my problem. Using the <strong>IRequiresSessionState</strong> interface in System.Web.SessionState, we got access to Session state.</p>
<p>According to Microsoft &#8220;<em>Specifies that the target HTTP handler requires read and write access to session-state values. This is a marker interface and has no methods.</em>&#8221;</p>
<p>Another option was to use the <strong>IReadOnlySessionState</strong> interface (Specifies that the target HTTP handler requires only read access to session-state values. This is a marker interface and has no methods.)</p>
<p>Needless to say, there is a performance increase in the latter case and if your requirement is only to read Session state, then that would be the right choice</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2008/09/26/generic-handler-session-state/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encypting query string in Asp.Net</title>
		<link>http://www.adnan-rashid.com/2008/07/08/encypting-query-string-in-asp-net/</link>
		<comments>http://www.adnan-rashid.com/2008/07/08/encypting-query-string-in-asp-net/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 04:00:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=18</guid>
		<description><![CDATA[When you pass information from one page to another, you are passing information that anybody can sniff. For example consider a scenario, in which you pass the customer id as a query string: http://www.yourapplication.com?customer_id=15 Now if somebody replaced 15 with say 10 or any other number, they can pull up other customer information. And that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>When you pass information from one page to another, you are passing information that anybody can sniff. For example consider a scenario, in which you pass the customer id as a query string:</p>
<p>http://www.yourapplication.com?customer_id=15</p>
<p>Now if somebody replaced 15 with say 10 or any other number, they can pull up other customer information. And that&#8217;s bad for security.</p>
<p>One solution to this problem is to use encryption using a secret key. So lets use a hard-to-crack 8 byte key like <strong>$zm0!qp?</strong></p>
<p>To accomplish this here is a code snippet</p>
<pre class="csharp">using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography;

public class Encryption64
{
    private byte[] key = {};
    private byte[] IV = {18, 52, 86, 120, 144, 171, 205, 239};

    public string Decrypt(string stringToDecrypt, string sEncryptionKey)
    {
		byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
		try
		{
			key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey, 8);
			DESCryptoServiceProvider des = new DESCryptoServiceProvider();
			inputByteArray = Convert.FromBase64String(stringToDecrypt);
			MemoryStream ms = new MemoryStream();
			CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
			cs.Write(inputByteArray, 0, inputByteArray.Length);
			cs.FlushFinalBlock();
			System.Text.Encoding encoding = System.Text.Encoding.UTF8;
			return encoding.GetString(ms.ToArray());
		}
		catch (Exception e)
		{
			return e.Message;
		}
    }

    public string Encrypt(string stringToEncrypt, string sEncryptionKey)
    {
		try
		{
			key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey, 8);
			DESCryptoServiceProvider des = new DESCryptoServiceProvider();
			byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
			MemoryStream ms = new MemoryStream();
			CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
			cs.Write(inputByteArray, 0, inputByteArray.Length);
			cs.FlushFinalBlock();
			return Convert.ToBase64String(ms.ToArray());
		}
		catch (Exception e)
		{
			return e.Message;
		}
    }
}
</pre>
<p><em>The end user will get to see a random text in the query string, something like</em></p>
<p><em>http://www.yourapplication.com/Receive.aspx?key=a2f5ckj?h79#8dd3</em></p>
<p><em>Remember stay secure stay safe.</em></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2008/07/08/encypting-query-string-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asp.Net Reset Password</title>
		<link>http://www.adnan-rashid.com/2008/07/06/asp-net-reset-password/</link>
		<comments>http://www.adnan-rashid.com/2008/07/06/asp-net-reset-password/#comments</comments>
		<pubDate>Sun, 06 Jul 2008 04:00:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blog.wtesolutions.com/?p=5</guid>
		<description><![CDATA[If you are using Membership Provider in your ASP.net application, you might come across a scenario in which you need to have both Security Question and Answer feature and would also like to pro-grammatically reset the password for an account. So if you run the code string username = "user"; string password = "password"; MembershipUser [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using Membership Provider in your ASP.net application, you might come across a scenario in which you need to have both Security Question and Answer feature and would also like to pro-grammatically reset the password for an account.</p>
<p>So if you run the code</p>
<pre class="csharp">string username = "user";
string password = "password";
MembershipUser mu = Membership.GetUser(username);
mu.ChangePassword(mu.ResetPassword(), password);
</pre>
<p>You will get an error when you try to reset the password. A solution is to add another Membership provider having all the same settings as the default provider with only one exception:</p>
<pre class="xml">&lt;add name="NewMembershipProvider" requiresQuestionAndAnswer="false" ....../&gt;
</pre>
<p>For all Membership functions, you the default Membership provider will be used, but when you need to reset the password, you must reference the new Provider. Here&#8217;s a code excerpt to help you out:</p>
<pre class="csharp">string username = "user";
string password = "password";
MembershipUser mu = Membership.Providers["NewMembershipProvider"].GetUser(username, false);
mu.ChangePassword(mu.ResetPassword(), password);
</pre>
<p>So now your application can use both the Security feature to recover password and pro-grammatically Change/Reset the password when required.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.adnan-rashid.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.adnan-rashid.com/2008/07/06/asp-net-reset-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
