<?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>Schotime.net &#187; C# MVC .net</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/c-mvc-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Thu, 01 Jul 2010 14:42:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Custom Authorization With Asp.net MVC</title>
		<link>http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/</link>
		<comments>http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 11:17:29 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# MVC .net]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/</guid>
		<description><![CDATA[The whole advantage with MVC over webforms is extensibility at every point. Extensibility, Extensibility, Extensibility.
Authorization is a very important and every web project has there own needs and requirements. Full customisation is paramount.
Here I will show you a simple way to customise your authorization.
In MVC attributes are used to protect a controller method, so we [...]]]></description>
			<content:encoded><![CDATA[<p>The whole advantage with MVC over webforms is extensibility at every point. Extensibility, Extensibility, Extensibility.</p>
<p>Authorization is a very important and every web project has there own needs and requirements. Full customisation is paramount.</p>
<p>Here I will show you a simple way to customise your authorization.</p>
<p>In MVC attributes are used to protect a controller method, so we to get started all we need to do is inherit from the AuthorizeAttribute class.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832">    public class </span><span style="background: black; color: #ffc66d">CustomAuthorizeAttribute </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AuthorizeAttribute
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">protected override bool </span><span style="background: black; color: white">AuthorizeCore(</span><span style="background: black; color: #ffc66d">HttpContextBase </span><span style="background: black; color: white">httpContext)
        {
            </span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">[] users = Users.Split(</span><span style="background: black; color: #a5c25c">','</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(!httpContext.User.Identity.IsAuthenticated)
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(users.Length &gt; </span><span style="background: black; color: #6897bb">0 </span><span style="background: black; color: white">&amp;&amp;
                !users.Contains(httpContext.User.Identity.Name,
                    </span><span style="background: black; color: #ffc66d">StringComparer</span><span style="background: black; color: white">.OrdinalIgnoreCase))
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">return true</span><span style="background: black; color: white">;
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This is the basics. We can put any logic we like in here and all we have to do is return false if for whatever reason the user should not be authorized. Then all you need to do is decorate the controller method with the new attribute as below.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">CustomAuthorize</span><span style="background: black; color: white">]
    </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Index()
    {
        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>From this simple example we can expand it with custom Roles.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">CustomAuthorizeAttribute </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AuthorizeAttribute
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: gray">// the "new" must be used here because we are overriding
        // the Roles property on the underlying class
        </span><span style="background: black; color: #cc7832">public new </span><span style="background: black; color: #6897bb">SiteRoles </span><span style="background: black; color: white">Roles;

        </span><span style="background: black; color: #cc7832">protected override bool </span><span style="background: black; color: white">AuthorizeCore(</span><span style="background: black; color: #ffc66d">HttpContextBase </span><span style="background: black; color: white">httpContext)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(httpContext == </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">)
                </span><span style="background: black; color: #cc7832">throw new </span><span style="background: black; color: #ffc66d">ArgumentNullException</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">"httpContext"</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">[] users = Users.Split(',');

</span><span style="background: black; color: white">            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(!httpContext.User.Identity.IsAuthenticated)
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #6897bb">SiteRoles </span><span style="background: black; color: white">role = (</span><span style="background: black; color: #6897bb">SiteRoles</span><span style="background: black; color: white">)httpContext.Session[</span><span style="background: black; color: #a5c25c">"role"</span><span style="background: black; color: white">];

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(</span><span style="background: black; color: white">Roles != 0 &amp;&amp; ((Roles</span><span style="background: black; color: white"> &amp; </span><span style="background: black; color: white">role) != role))</span><span style="background: black; color: white">
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">return true</span><span style="background: black; color: white">;
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Where the SiteRoles class is defined as below.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">Serializable</span><span style="background: black; color: white">]
    [</span><span style="background: black; color: #ffc66d">Flags</span><span style="background: black; color: white">]
    </span><span style="background: black; color: #cc7832">public enum </span><span style="background: black; color: #6897bb">SiteRoles
    </span><span style="background: black; color: white">{
        User = </span><span style="background: black; color: #6897bb">1 </span><span style="background: black; color: white">&lt;&lt; </span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">,
        Admin = </span><span style="background: black; color: #6897bb">1 </span><span style="background: black; color: white">&lt;&lt; </span><span style="background: black; color: #6897bb">1</span><span style="background: black; color: white">,
        Helpdesk = </span><span style="background: black; color: #6897bb">1 </span><span style="background: black; color: white">&lt;&lt; </span><span style="background: black; color: #6897bb">2
    </span><span style="background: black; color: white">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This can then be used be used as follows.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">CustomAuthorize</span><span style="background: black; color: white">(Roles=</span><span style="background: black; color: #6897bb">SiteRoles</span><span style="background: black; color: white">.Admin|</span><span style="background: black; color: #6897bb">SiteRoles</span><span style="background: black; color: white">.HelpDesk)]
    </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Index()
    {
        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This will only allow the Admin and the Helpdesk Role access to the Index controller. If you don&#8217;t belong to one of these roles then you will be sent to the Login page.</p>
<p>The possibilities are really endless.<br />
Happy coding.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
