title: MVC 5- changing code infrastructure from old Forms authentication to OWIN forms authentication
body: <p>I'm trying to migrate an old .NET 3.5, MVC 1 project to a new .NET 4.5 MVC 5 project. I have run into a road block where there is old security/authentication infrastructure in place and I am not quite sure on how to change it over to OWIN claims based authentication. I am still trying to maintain FormsAuthentication with OWIN. A couple of classes:</p>
  
  <p><strong>DomainPrincipal.cs</strong></p>
  
  <pre><code>public sealed class DomainPrincipal : MarshalByRefObject, IPrincipal
  {
      private readonly IIdentity _identity;
      private readonly User _user;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="DomainPrincipal"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="identity"&gt;The identity.&lt;/param&gt;
      /// &lt;param name="user"&gt;The user.&lt;/param&gt;
      public DomainPrincipal(IIdentity identity, User user)
      {
          _identity = identity;
          _user = user;
      }
  
      /// &lt;summary&gt;
      /// Gets the user.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The &lt;see cref="User"/&gt; associated with the current principal.&lt;/value&gt;
      public User User
      {
          get { return _user; }
      }
  
      #region IPrincipal Members
  
      /// &lt;summary&gt;
      /// Determines whether the current principal belongs to the specified role.
      /// &lt;/summary&gt;
      /// &lt;returns&gt;
      /// &lt;c&gt;true&lt;/c&gt; if the current principal is a member of the specified role; otherwise &lt;c&gt;false&lt;/c&gt;.
      /// &lt;/returns&gt;
      /// &lt;param name="roleName"&gt;The name of the role for which to check membership. &lt;/param&gt;
      public Boolean IsInRole(String roleName)
      {
          return User.HasPrivilege(roleName);
      }
  
      /// &lt;summary&gt;
      /// Gets the identity of the current principal.
      /// &lt;/summary&gt;
      /// &lt;value&gt;&lt;/value&gt;
      /// &lt;returns&gt;The &lt;see cref="T:System.Security.Principal.IIdentity"/&gt; object associated with the current principal.&lt;/returns&gt;
      public IIdentity Identity
      {
          get { return _identity; }
      }
  
      #endregion
  }
  </code></pre>
  
  <p><strong>WebFormsAuthenticatedContext.cs</strong> - uses DomainPrincipal class to implement AuthenticatedContext for a forms authentication environment.</p>
  
  <pre><code>public sealed class WebFormsAuthenticatedContext : AuthenticatedContext
  {
      /// &lt;summary&gt;
      /// Gets the current user.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The current user.&lt;/value&gt;
      /// &lt;remarks&gt;Determines the current user from the user principal of the current &lt;see cref="HttpContext"/&gt;.&lt;/remarks&gt;
      public User User
      {
          get { return IsValid ? ((DomainPrincipal)HttpContext.Current.User).User : null; }
      }
  
      /// &lt;summary&gt;
      /// Gets a value indicating whether the &lt;see cref="User"/&gt; property is currently valid.
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      ///     &lt;c&gt;true&lt;/c&gt; if the &lt;see cref="User"/&gt; property is valid; otherwise, &lt;c&gt;false&lt;/c&gt;.
      /// &lt;/value&gt;
      public Boolean IsValid
      {
          get { return HttpContext.Current != null &amp;&amp; HttpContext.Current.User is DomainPrincipal; }
      }
  }
  </code></pre>
  
  <p><strong>ServiceAuthenticatedContext.cs</strong> - Implements AuthenticatedContext for a WCF service environment.</p>
  
  <pre><code>public class ServiceAuthenticatedContext : AuthenticatedContext
  {
      /// &lt;summary&gt;
      /// Gets or sets the user DAO.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The user DAO.&lt;/value&gt;
      public UserDao UserDao { get; set; }
  
      #region AuthenticatedContext Members
  
      /// &lt;summary&gt;
      /// Gets the current user.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The current user.&lt;/value&gt;
      /// &lt;remarks&gt;Determines the user identity from the current &lt;see cref="ServiceSecurityContext"/&gt;.&lt;/remarks&gt;
      public User User
      {
          get
          {
              const string AuthorizationContextKey = "DomainPrincipal";
  
              AuthorizationContext authorizationContext = ServiceSecurityContext.Current.AuthorizationContext;
              User user;
  
              if (authorizationContext.Properties.ContainsKey(AuthorizationContextKey))
              {
                  user = authorizationContext.Properties[AuthorizationContextKey] as User;
              }
              else
              {
                  string username = ServiceSecurityContext.Current.PrimaryIdentity.Name;
                  user = UserDao.GetByUsername(username);
                  authorizationContext.Properties.Add(AuthorizationContextKey, user);
              }
              return user;
          }
      }
  
      public String Username { get; private set; }
  
      /// &lt;summary&gt;
      /// Gets a value indicating whether the &lt;see cref="User"/&gt; property is currently valid.
      /// &lt;/summary&gt;
      /// &lt;value&gt;
      ///     &lt;c&gt;true&lt;/c&gt; if the &lt;see cref="User"/&gt; property is valid; otherwise, &lt;c&gt;false&lt;/c&gt;.
      /// &lt;/value&gt;
      public bool IsValid
      {
          get { return ServiceSecurityContext.Current.PrimaryIdentity != null &amp;&amp; ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated; }
      }
  
      #endregion
  }
  </code></pre>
  
  <p>I am now looking at achieving something similar using OWIN, signing the user in like so:</p>
  
  <pre><code>// Log user in
              var identity = new ClaimsIdentity(new[] 
                      {
                          new Claim(ClaimTypes.Name, model.Username),
                      },
                      DefaultAuthenticationTypes.ApplicationCookie,
                      ClaimTypes.Name, ClaimTypes.Role);
  
              Authentication.SignIn(new AuthenticationProperties
              {
                  IsPersistent = model.RememberMe
              }, identity);
  </code></pre>
  
  <p>and using the custom DomainPrincipal class in place as a ClaimsPrincipal instead (changing it accordingly) and changing WebFormsAuthenticatedContext and ServiceAuthenticatedContext classes to work with this new DomainPrincipal and OWIN security model.</p>
  
  <p>Is this possible to do? Or would I need to revamp the whole authentication infrastructure completely?</p>
  
  <p>Your help and advice would be much appreciated.</p>
  
  <p>Thank you.</p>