title: Customizing .net Identity 2
body: <p>I have an MVC5 application with .net 4.7.2 and I'm trying to have my customized version of .net Identity classes. I've followed the steps in this tutorial, with some changes here and there, because my project rely on a set of WCF services to communicate with the database, so I don't have direct connection to the database:
  <a href="https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity" rel="nofollow noreferrer">overview-of-custom-storage-providers-for-aspnet-identity</a>
  So mainly I've created my own MyUserStore which implements IUserStore and IUserRoleStore, and I've added this to Startup.cs:</p>
  
  <pre><code>public void ConfigureAuth(IAppBuilder app)
  {
       app.CreatePerOwinContext&lt;MyUserManager&gt;(MyUserManager.Create);
  }
  </code></pre>
  
  <p>Now the problem that the IsInRole methods are not getting invoked, and also I had break point for methods like FindByIdAsync, FindByNameAsync and IsInRoleAsync and nothing got invoked, even though I've added 
  <code>[Authorize(Roles ="TestRole")]</code> attribute to couple actions and has tried to invoke this explicitly:</p>
  
  <pre><code>this.User.IsInRole("TestRole");
  </code></pre>
  
  <p>What I'm missing here to have it work in the proper way?</p>
  
  <p>Edit #1 - adding the whole code:
  Here under is the User Store related classes:</p>
  
  <pre><code>public class MyUserStore :
              IUserStore&lt;MyIdentityUser, int&gt;,
              IUserRoleStore&lt;MyIdentityUser, int&gt;,
              IQueryableUserStore&lt;MyIdentityUser, int&gt;,
              IDisposable
      {
  
          public Task CreateAsync(MyIdentityUser account)
          {
              // Add account to DB...
              return Task.FromResult&lt;object&gt;(null);
          }
  
          public Task&lt;MyIdentityUser&gt; FindByIdAsync(int accountId)
          {
              // Get account from DB and return it 
              return Task.FromResult&lt;MyIdentityUser&gt;(account);
          }
  
          public Task&lt;MyIdentityUser&gt; FindByNameAsync(string userName)
          {
              // Get account from DB and return it 
              return Task.FromResult&lt;MyIdentityUser&gt;(account);
          }
  
          public Task UpdateAsync(MyIdentityUser account)
          {
              // Update account in DB 
              return Task.FromResult&lt;object&gt;(null);
          }
  
          public Task AddToRoleAsync(MyIdentityUser account, string roleName)
          {
              throw new NotImplementedException("UserStore.AddToRoleAsync");
              return Task.FromResult&lt;object&gt;(null);
          }
  
          public Task&lt;IList&lt;string&gt;&gt; GetRolesAsync(MyIdentityUser account)
          {
              // TODO: Check if important to implement
              throw new NotImplementedException("UserStore.GetRolesAsync");
          }
  
          public Task&lt;bool&gt; IsInRoleAsync(MyIdentityUser account, string task)
          {
              // Return true if has permission (not getting invoked)
              return Task.FromResult&lt;bool&gt;(hasPermission);
          }
  
           public Task RemoveFromRoleAsync(MyIdentityUser account, string task)
          {
              throw new NotImplementedException("UserStore.RemoveFromRoleAsync");
          }
  
          public Task DeleteAsync(MyIdentityUser account)
          {
              // Delete user from DB
              return Task.FromResult&lt;Object&gt;(null);
          }    
      }
  
      public class MyUserRole : IdentityUserRole&lt;int&gt; { }
      public class MyUserClaim : IdentityUserClaim&lt;int&gt; { }
      public class MyUserLogin : IdentityUserLogin&lt;int&gt; { }
      public class MyIdentityUser : IdentityUser&lt;int, MyUserLogin, MyUserRole, MyUserClaim&gt;
      {
          public MyIdentityUser(int id)
          {
              Id = id;
          }
  
          // My extra account's properties
      }
  }
  </code></pre>
  
  <p>And here is the UserManager class:</p>
  
  <pre><code>public class MyUserManager : UserManager&lt;MyIdentityUser, int&gt;
  {
      public MyUserManager(IUserStore&lt;MyIdentityUser, int&gt; store)
          : base(store) { }
  
      public static MyUserManager Create(IdentityFactoryOptions&lt;MyUserManager&gt; options, IOwinContext context)
      {
          ///Calling the non-default constructor of the UserStore class
          var manager = new MyUserManager(new MyUserStore());
  
          manager.UserValidator = new UserValidator&lt;MyIdentityUser, int&gt;(manager)
          {
              AllowOnlyAlphanumericUserNames = false,
              RequireUniqueEmail = true
          };
  
          // Configure validation logic for passwords
          manager.PasswordValidator = new PasswordValidator
          {
              RequiredLength = 6,
          };
  
          // Configure user lockout defaults
          manager.UserLockoutEnabledByDefault = false;
          manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
          manager.MaxFailedAccessAttemptsBeforeLockout = 3;
  
          // You can write your own provider and plug it in here.
          return manager;
      }
  }
  </code></pre>
  
  <p>And maybe this is the most important part, the login controller:</p>
  
  <pre><code>var manager = HttpContext.GetOwinContext().GetUserManager&lt;MyUserManager&gt;();
  //check for credentials before sign in ..    
  var result = manager.CheckPasswordAsync(vm.userName, vm.password, ref state);
  if(result.Result)
  { 
      FormsAuthentication.SetAuthCookie(vm.userName, vm.rememberMe);
      return Redirect("~/");
  }
  </code></pre>
  
  <p>Regards,</p>