title: Identity 2.0: Creating custom ClaimsIdentity eg: User.Identity.GetUserById<int>(int id) for Per Request Validation
body: <p>See this similar question: <a href="http://aspnetidentity.codeplex.com/workitem/2263" rel="nofollow noreferrer">Need access more user properties in User.Identity</a></p>
  <p>I would like to create custom authentication methods to use with my Razor Views that allows easy access <code>IdentityUser</code> properties relational to the User.Identity object but I am not sure how to go about it. I want to create several custom extensions similar to <code>User.Identity.GetUserName()</code>, <code>User.Identity.GetUserById()</code>, etc... instead of using this <a href="http://www.dotnetexpertguide.com/2012/12/aspnet-mvc-accessing-base-controller.html" rel="nofollow noreferrer">ViewContextExtension</a> method. My Authentication type is currently the default type <code>DefaultAuthenticationTypes.ApplicationCookie</code> from VS2013 MVC5 template. As Shoe stated, I need this claim to be inserted after the user signs in.</p>
  <p>My questions is:</p>
  <p><strong>How and where do you create a custom claim that has an out parameter of <code>this IIdentity</code> under IPrincipal?</strong></p>
  <p>This would allow me to access User properties via CookieAuthentication in a View for entities in a DDD setting where I have multiple DbContexts in a single app using Identity 2.0. I will eventually use WebAPI, but for now I want to keep it as simple as possible. I have found <a href="https://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal">this SO Q&amp;A</a> but it is geared towards Web Forms using Tickets. Not sure the difference between tickets and tokens either?</p>
  <p>This is the current approach that uses <code>ViewContext</code> from a base controller:</p>
  <p><strong>View:</strong></p>
  <pre><code>    @using Microsoft.AspNet.Identity
      @using Globals.Helpers
      @using Identity //custom Identity for Domain
      @using Microsoft.AspNet.Identity.Owin
      
      @if (Request.IsAuthenticated)
      {
            var url = @ViewContext.BaseController().GetAvatarUrlById(User.Identity.GetUserId&lt;int&gt;());
    
          //...
      }
  </code></pre>
  <p><strong>BaseController.cs</strong></p>
  <pre><code>        public string GetAvatarUrlById(int id)
          {
  
              var user = UserManager.FindById(id);
  
              return &quot;../../&quot; + user.ImageUrl;
          }
  </code></pre>
  <p><strong>Extensions.cs</strong></p>
  <pre><code>    public static class ViewContextExtension
      {
          public static BaseController BaseController(this ViewContext view)
          {
              var baseController = (BaseController)view.Controller;
              return baseController;
          }
      }
  </code></pre>
  <p>What I am looking for is but where &amp; how?</p>
  <p><strong>View:</strong></p>
  <pre><code>&lt;img src=&quot;@User.Identity.GetAvatarUrl()&quot; alt=&quot;User.Identity.GetAvatarUrl()&quot; /&gt;
  </code></pre>
  <p><strong>SOLUTION</strong></p>
  <p>I simply edited the <strong>Extension.cs</strong> file and used inheritance for the Base controller which is used for the _LoginPartial.cshtml &amp; edited the <code>ViewContextExtension</code> class:</p>
  <pre><code>    #region ViewContextExt
      public static class ViewContextExtension
      {
          public static BaseController BaseController(this ViewContext view)
          {
              var baseController = (BaseController)view.Controller;
              return baseController;
          }
  
          public static string GetAvatarUrl(this IIdentity identity)
          {
              return ((ClaimsIdentity)identity).Claims.First(c =&gt; c.Type == &quot;AvatarUrl&quot;).Value;
          }
      }
  }
  # endregion
  </code></pre>