<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>TJ Solutions - ASP.NET</title>
    <link>http://www.tjsolutions.nl/</link>
    <description />
    <language>en-us</language>
    <copyright>Tijmen van de Kamp, Tom de Koning</copyright>
    <lastBuildDate>Thu, 01 Oct 2009 17:36:27 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>blog@tjsolutions.nl</managingEditor>
    <webMaster>blog@tjsolutions.nl</webMaster>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=19fd90fc-5243-427f-9875-51e90003dc22</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</pingback:target>
      <dc:creator>Tijmen</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=19fd90fc-5243-427f-9875-51e90003dc22</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Just a quick note: there is an update out
for the latest .NET framework version, that addresses issues with ASP.NET dynamic
data. Amongst others, the site will not display foreign key fields in a one-to-one
relationship. This <a href="http://support.microsoft.com/kb/959209">KB article</a> contains
all the download links to the various versions.<br />
Not a new issue by any means (the fix is dated January 2009), but good to know nonetheless.<br /><p></p><img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=19fd90fc-5243-427f-9875-51e90003dc22" /></body>
      <title>Update to .NET 3.5 sp1 required for dynamic data</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/10/01/UpdateToNET35Sp1RequiredForDynamicData.aspx</link>
      <pubDate>Thu, 01 Oct 2009 17:36:27 GMT</pubDate>
      <description>Just a quick note: there is an update out for the latest .NET framework version, that addresses issues with ASP.NET dynamic data. Amongst others, the site will not display foreign key fields in a one-to-one relationship. This &lt;a href="http://support.microsoft.com/kb/959209"&gt;KB
article&lt;/a&gt; contains all the download links to the various versions.&lt;br&gt;
Not a new issue by any means (the fix is dated January 2009), but good to know nonetheless.&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=19fd90fc-5243-427f-9875-51e90003dc22" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,19fd90fc-5243-427f-9875-51e90003dc22.aspx</comments>
      <category>ASP.NET</category>
    </item>
    <item>
      <trackback:ping>http://www.tjsolutions.nl/Trackback.aspx?guid=827c1f80-2f30-46db-b29e-6bfd62c6731e</trackback:ping>
      <pingback:server>http://www.tjsolutions.nl/pingback.aspx</pingback:server>
      <pingback:target>http://www.tjsolutions.nl/PermaLink,guid,827c1f80-2f30-46db-b29e-6bfd62c6731e.aspx</pingback:target>
      <dc:creator>Tijmen</dc:creator>
      <wfw:comment>http://www.tjsolutions.nl/CommentView,guid,827c1f80-2f30-46db-b29e-6bfd62c6731e.aspx</wfw:comment>
      <wfw:commentRss>http://www.tjsolutions.nl/SyndicationService.asmx/GetEntryCommentsRss?guid=827c1f80-2f30-46db-b29e-6bfd62c6731e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Every now and then I want to do a simple authentication scheme in an ASP.NET web application,
without calling on various providers or other components that bring too much weight
&amp; functionality to the table. Implementing a custom <code>IIdentity</code> and <code>IPrincipal</code> is
a straightforward way of achieving this.
</p>
        <p>
The steps involved:
</p>
        <ul>
          <li>
create a custom implementation of these two interfaces 
</li>
          <li>
set the custom principal as the user for <code>HttpContext.Current</code></li>
          <li>
cache the principal in session state (not actually required, but a simple optimization) 
</li>
        </ul>
        <p>
          <strong>Implementing the interfaces</strong>
        </p>
        <p>
A simple skeleton for the <code>IIdentity</code> implementation:
</p>
        <pre class="brush: csharp;">public class MyIdentity : IIdentity
{
    private string passedInName; 

    // IIdentity
    public string AuthenticationType { get { return "MyIdentityAuthenticationType"; } }
    public string Name { get { return passedInName; } }

    public bool IsAuthenticated
    {
        get
        {
            // TODO: add whatever logic we need here (for instance, AD group membership)
            throw new NotImplementedException();
        }
    }
    // /IIdentity

    // Hide constructor
    private MyIdentity() { }

    // Static factory method
    internal static IIdentity CreateIdentity(IUserNameProvider provider)
    {
        IIdentity identity = new MyIdentity { passedInName = provider.AccountName } ;
        return identity;
    }
}</pre>
        <p>
And for the <code>IPrincipal</code>-implementing class:
</p>
        <pre class="brush: csharp;">public class MyPrincipal : IPrincipal
{
    private IIdentity identity;

    // IPrincipal
    public IIdentity Identity    { get { return identity; } }
    public bool IsInRole(string role)
    {
        // TODO add logic here
        throw new NotImplementedException();
    }

    // hide Ctor
    private MyPrincipal() { }

    // static factory method
    public static IPrincipal CreatePrincipal(IUserNameProvider provider)
    {
        IIdentity identity = MyIdentity.CreateIdentity(provider);
        IPrincipal principal = new MyPrincipal { identity = identity };
        return principal;
    }
}</pre>
        <p>
A few things to note:
</p>
        <ul>
          <li>
            <code>IUserNameProvider</code> has only a single string property, name. The injected
type is used to determine the current user's name. 
</li>
          <li>
I used simple static factory methods, that are by no means necessary in this context
(regular constructors would work just as well). 
</li>
          <li>
you could factor out the two "business logic" methods <code>IsAuthenticated</code> and <code>IsInRole</code> to
separate concerns using the template method pattern, but this is supposed to be a
simple, lean implementation. 
</li>
        </ul>
        <p>
          <strong>Hooking them up</strong>
        </p>
        <p>
The easiest way I've found to link the custom principal is by implementing the <code>Application_AcquireRequestState</code> event
in the global.asax:
</p>
        <pre class="brush: csharp;">protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    IPrincipal principal = null;

    var current = HttpContext.Current;

    // check if session is available; needed because this event can/does fire multiple times per request
    if (current.Session != null)
    {
        // check if we have the principal in session
        if (current.Session["magicstring"] != null)
        {
            principal = current.Session["magicstring"] as IPrincipal;
        }
        // catches both (1) key not in session and (2) cast not succesfull
        if (principal == null)
        {
            // create a new one and store it in the session
            principal = MyPrincipal.CreatePrincipal(new MyWebAccountNameProvider());
            current.Session["magicstring"] = principal;
        }

        // regardless of source (session or freshly created, store in appropriate context)
        current.User = principal;
    }
}</pre>
        <p>
Actually, this event is a bit weird, because it can fire multiple times for a single
request. The Session reference is only valid once however, which is why that check
is there. So, what happens:
</p>
        <ol>
          <li>
The existence of a session variable is checked, and, if it exists, it is cast to an
IPrincipal. 
</li>
          <li>
If either the cast failed or the session var was empty, a new specific principal is
constructed and stored in session. 
</li>
          <li>
The custom principal is linked to the HttpContext.Current.User property, allowing
us to use it in the rest of the application. 
</li>
        </ol>
        <p>
          <strong>Wrapup and comments</strong>
        </p>
        <p>
This is by no means a do-all, end-all solution, but it works well. Depending on the
tiering/layering of the solution, you can either check the <code>HttpContext.Current.User</code> property,
or you can abstract that into a simple wrapper. I usually either use a static method
on my principal that returns the actual active principal, or set up a context registry
to hold my principal reference transparently across layers.
</p>
        <img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=827c1f80-2f30-46db-b29e-6bfd62c6731e" />
      </body>
      <title>Simple ASP.NET IIdentity/IPrincipal implementation</title>
      <guid isPermaLink="false">http://www.tjsolutions.nl/PermaLink,guid,827c1f80-2f30-46db-b29e-6bfd62c6731e.aspx</guid>
      <link>http://www.tjsolutions.nl/2009/09/02/SimpleASPNETIIdentityIPrincipalImplementation.aspx</link>
      <pubDate>Wed, 02 Sep 2009 20:57:31 GMT</pubDate>
      <description>&lt;p&gt;
Every now and then I want to do a simple authentication scheme in an ASP.NET web application,
without calling on various providers or other components that bring too much weight
&amp;amp; functionality to the table. Implementing a custom &lt;code&gt;IIdentity&lt;/code&gt; and &lt;code&gt;IPrincipal&lt;/code&gt; is
a straightforward way of achieving this.
&lt;/p&gt;
&lt;p&gt;
The steps involved:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
create a custom implementation of these two interfaces 
&lt;/li&gt;
&lt;li&gt;
set the custom principal as the user for &lt;code&gt;HttpContext.Current&lt;/code&gt; 
&lt;/li&gt;
&lt;li&gt;
cache the principal in session state (not actually required, but a simple optimization) 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Implementing the interfaces&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A simple skeleton for the &lt;code&gt;IIdentity&lt;/code&gt; implementation:
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;public class MyIdentity : IIdentity
{
    private string passedInName; 

    // IIdentity
    public string AuthenticationType { get { return &amp;quot;MyIdentityAuthenticationType&amp;quot;; } }
    public string Name { get { return passedInName; } }

    public bool IsAuthenticated
    {
        get
        {
            // TODO: add whatever logic we need here (for instance, AD group membership)
            throw new NotImplementedException();
        }
    }
    // /IIdentity

    // Hide constructor
    private MyIdentity() { }

    // Static factory method
    internal static IIdentity CreateIdentity(IUserNameProvider provider)
    {
        IIdentity identity = new MyIdentity { passedInName = provider.AccountName } ;
        return identity;
    }
}&lt;/pre&gt;
&lt;p&gt;
And for the &lt;code&gt;IPrincipal&lt;/code&gt;-implementing class:
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;public class MyPrincipal : IPrincipal
{
    private IIdentity identity;

    // IPrincipal
    public IIdentity Identity    { get { return identity; } }
    public bool IsInRole(string role)
    {
        // TODO add logic here
        throw new NotImplementedException();
    }

    // hide Ctor
    private MyPrincipal() { }

    // static factory method
    public static IPrincipal CreatePrincipal(IUserNameProvider provider)
    {
        IIdentity identity = MyIdentity.CreateIdentity(provider);
        IPrincipal principal = new MyPrincipal { identity = identity };
        return principal;
    }
}&lt;/pre&gt;
&lt;p&gt;
A few things to note:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IUserNameProvider&lt;/code&gt; has only a single string property, name. The injected
type is used to determine the current user's name. 
&lt;/li&gt;
&lt;li&gt;
I used simple static factory methods, that are by no means necessary in this context
(regular constructors would work just as well). 
&lt;/li&gt;
&lt;li&gt;
you could factor out the two "business logic" methods &lt;code&gt;IsAuthenticated&lt;/code&gt; and &lt;code&gt;IsInRole&lt;/code&gt; to
separate concerns using the template method pattern, but this is supposed to be a
simple, lean implementation. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Hooking them up&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The easiest way I've found to link the custom principal is by implementing the &lt;code&gt;Application_AcquireRequestState&lt;/code&gt; event
in the global.asax:
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    IPrincipal principal = null;

    var current = HttpContext.Current;

    // check if session is available; needed because this event can/does fire multiple times per request
    if (current.Session != null)
    {
        // check if we have the principal in session
        if (current.Session[&amp;quot;magicstring&amp;quot;] != null)
        {
            principal = current.Session[&amp;quot;magicstring&amp;quot;] as IPrincipal;
        }
        // catches both (1) key not in session and (2) cast not succesfull
        if (principal == null)
        {
            // create a new one and store it in the session
            principal = MyPrincipal.CreatePrincipal(new MyWebAccountNameProvider());
            current.Session[&amp;quot;magicstring&amp;quot;] = principal;
        }

        // regardless of source (session or freshly created, store in appropriate context)
        current.User = principal;
    }
}&lt;/pre&gt;
&lt;p&gt;
Actually, this event is a bit weird, because it can fire multiple times for a single
request. The Session reference is only valid once however, which is why that check
is there. So, what happens:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
The existence of a session variable is checked, and, if it exists, it is cast to an
IPrincipal. 
&lt;/li&gt;
&lt;li&gt;
If either the cast failed or the session var was empty, a new specific principal is
constructed and stored in session. 
&lt;/li&gt;
&lt;li&gt;
The custom principal is linked to the HttpContext.Current.User property, allowing
us to use it in the rest of the application. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;strong&gt;Wrapup and comments&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
This is by no means a do-all, end-all solution, but it works well. Depending on the
tiering/layering of the solution, you can either check the &lt;code&gt;HttpContext.Current.User&lt;/code&gt; property,
or you can abstract that into a simple wrapper. I usually either use a static method
on my principal that returns the actual active principal, or set up a context registry
to hold my principal reference transparently across layers.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.tjsolutions.nl/aggbug.ashx?id=827c1f80-2f30-46db-b29e-6bfd62c6731e" /&gt;</description>
      <comments>http://www.tjsolutions.nl/CommentView,guid,827c1f80-2f30-46db-b29e-6bfd62c6731e.aspx</comments>
      <category>ASP.NET</category>
    </item>
  </channel>
</rss>