title: using custom IPrincipal and IIdentity in MVC3
body: <p>I create my own <code>IPrincipal</code> and <code>IIdentity</code> implementation as shown below:</p>
  
  <pre><code>[ComVisible(true)]
  [Serializable]
  public sealed class CustomIdentity : IIdentity {
  
      private readonly string _name;
      private readonly string _email;
      // and other stuffs
  
      public CustomIdentity(string name) {
          _name = name.Trim();
          if(string.IsNullOrWhiteSpace(name))
              return;
          _email = (connect to database and read email and other stuffs);
      }
  
      public string Name {
          get { return _name; }
      }
  
      public string Email {
          get { return _email; }
      }
  
      public string AuthenticationType {
          get { return "CustomIdentity"; }
      }
  
      public bool IsAuthenticated {
          get { return !string.IsNullOrWhiteSpace(_name); }
      }
  
  }
  
  
  [ComVisible(true)]
  [Serializable]
  public sealed class CustomPrincipal : IPrincipal {
  
      private readonly CustomIdentity _identity;
  
      public CustomPrincipal(CustomIdentity identity) {
          _identity = identity;
      }
  
      public bool IsInRole(string role) {
          return _identity != null &amp;&amp; 
                 _identity.IsAuthenticated &amp;&amp;
                 !string.IsNullOrWhiteSpace(role) &amp;&amp;
                 Roles.IsUserInRole(_identity.Name, role);
      }
  
      IIdentity IPrincipal.Identity {
          get { return _identity; }
      }
  
      public CustomIdentity Identity {
          get { return _identity; }
      }
  
  }
  </code></pre>
  
  <p>Also, I create a <code>HttpModule</code> and in its <code>AuthenticateRequest</code> event, I do this:</p>
  
  <pre><code>    public void Init(HttpApplication context) {
          _application = context;
          _application.AuthenticateRequest += ApplicationAuthenticateRequest;
      }
  
      private void ApplicationAuthenticateRequest(object sender, EventArgs e) {
          var formsCookie = _application.Request.Cookies[FormsAuthentication.FormsCookieName];
          var identity = formsCookie != null
               ? new CustomIdentity(FormsAuthentication.Decrypt(formsCookie.Value).Name)
               : new CustomIdentity(string.Empty);
          var principal = new CustomPrincipal(identity);
          _application.Context.User = Thread.CurrentPrincipal = principal;
      }
  </code></pre>
  
  <p>Also, I create my own <code>Controller</code> and <code>WebViewPage</code> like these:</p>
  
  <pre><code>public abstract class CustomController : Controller {
      public new CustomPrincipal User {
          get {
              var user = System.Web.HttpContext.Current.User as CustomPrincipal;
              return user;
          }
      }
  }
  
  
  public abstract class CustomWebViewPage&lt;TModel&gt; : WebViewPage&lt;TModel&gt; {
      public new CustomPrincipal User {
          get {
              // (Place number 1) here is the error I'm speaking about!!!
              var user = HttpContext.Current.User as CustomPrincipal;
              return user;
          }
      }
  }
  </code></pre>
  
  <p>as shown in above code, it seems everything is right; But as you can see, in <strong>Place number 1</strong> I can't access the <code>CustomPrincipal</code>! Means in this place, I have a <code>RolePrincipal</code> instead of having a <code>CustomPrincipal</code>. e.g. <code>HttpContext.Current.User</code> is a <code>RolePrincipal</code> instead of <code>CustomPrincipal</code>. But the <code>RolePrincipal.Identity</code> property is a <code>CustomIdentity</code>!</p>