<?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>ChillAxen.com &#187; asp.net</title>
	<atom:link href="http://www.chillaxen.com/category/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chillaxen.com</link>
	<description>Everything about anything!</description>
	<lastBuildDate>Sun, 06 Feb 2011 20:47:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>ASP.net &#8211; Force a user offline as admin (destroy a session by username)</title>
		<link>http://www.chillaxen.com/2011/02/asp-net-force-a-user-offline-as-admin-destroy-a-session-by-username/</link>
		<comments>http://www.chillaxen.com/2011/02/asp-net-force-a-user-offline-as-admin-destroy-a-session-by-username/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 20:46:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[membership]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.chillaxen.com/?p=32</guid>
		<description><![CDATA[So today I came across an issue.  I created a membership system as I always do, which included allowing the admin to delete specific users.  Worked perfectly, but problem was, if a user is already logged in, it did not kill their session.  So basically this user is able to stay online even if they [...]]]></description>
			<content:encoded><![CDATA[<p>So today I came across an issue.  I created a membership system as I always do, which included allowing the admin to delete specific users.  Worked perfectly, but problem was, if a user is already logged in, it did not kill their session.  So basically this user is able to stay online even if they were deleted.</p>
<p>After asking people on Stack Overflow and bouncing around some ideas I decided on the following solution</p>
<ol>
<li>In my global.aspx &#8220;Application_Start&#8221; I created a new variable to hold a list of strings, so I added:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">application<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;deleted_users&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> List<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Of</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>Next, when I deleted a user, I injected them into the application&#8217;s list of deleted users my calling:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">System.<span style="color: #0000FF;">Web</span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Application</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;deleted_users&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">add</span><span style="color: #000000;">&#40;</span>username<span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>Finally, I needed to make sure when a user makes a request, we checked against this list of users who were deleted.  If they are in the list, kill their session.  To do this I added the following to my Global.asax:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> MvcApplication_PreSendRequestHeaders<span style="color: #000000;">&#40;</span>sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">EventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">PreSendRequestHeaders</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">'This will make sure if we are logged on, we dont need to get logged off</span>
  <span style="color: #0600FF;">If</span> User.<span style="color: #0000FF;">Identity</span>.<span style="color: #0000FF;">IsAuthenticated</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span> <span style="color: #FF8000;">Then</span> <span style="color: #0600FF;">Exit</span> <span style="color: #0600FF;">Sub</span>
  <span style="color: #0600FF;">If</span> Application<span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;deleted_users&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">contains</span><span style="color: #000000;">&#40;</span>User.<span style="color: #0000FF;">Identity</span>.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
    PrivateObjects.<span style="color: #0000FF;">Classes</span>.<span style="color: #0000FF;">Memberships</span>.<span style="color: #0000FF;">Logout</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>
&nbsp;
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></pre></td></tr></table></div>

</li>
</ol>
<p>Something to note is by using the PreSendRequestHeaders, it processes after the page is already being sent, so the user will be allowed one more page view before his session is destroyed.  I am sure there is a different method you can use in your Global.asax that will do this at the first post, but that was enough for me!</p>
<p>Hope this helps anyone else who was having this issue</p>
]]></content:encoded>
			<wfw:commentRss>http://www.chillaxen.com/2011/02/asp-net-force-a-user-offline-as-admin-destroy-a-session-by-username/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

