title: ASP.net MVC + ASP.net Identity Seeding roles and users
body: <p>In Migrations Configuration class, there is Seed method, and it looks like:</p>
  
  <pre><code>protected override void Seed(DatabaseContext context)
          {
             var  roleManager = new RoleManager&lt;IdentityRole&gt;(new RoleStore&lt;IdentityRole&gt;(context));
             var  userManager = new UserManager&lt;User&gt;(new UserStore&lt;User&gt;(context)); 
  
              if (!roleManager.RoleExists("Administrator"))
              {
                  roleManager.Create(new IdentityRole("Administrator"));
              }
  
              var user = new User {UserName = "someUserName"};
  
  
              if (userManager.FindByName("someUserName") == null)
              {
                  var result = userManager.Create(user, "password");
  
                  if (result.Succeeded)
                  {
                      userManager.AddToRole(user.Id, "Administrator");
                  }    
  
              }
              }
  </code></pre>
  
  <p>And then in PM console I've run update-database, and it successfully  seeded  database, and in table AspNetUserRoles I can see that user someUserName and Administrator role are connected.</p>
  
  <p>But when I put attribute <code>[Authorize(Roles = "Administrator")]</code> above controller I've been redirected to log in page as if my user wasn't in that role, so I've  added line in my controller:</p>
  
  <pre><code>var x = _userManager.IsInRole(_userManager.FindByName("someUserName").Id, "Administrator");
  </code></pre>
  
  <p>And x is false. Why is this happening ? And how can I fix it ? And if this isn't proper way to seed database, which is ? </p>