locked
ASP.NET Identity 3.0 Entity Framework Database Seeding RRS feed

  • Question

  • User1474298650 posted

    I'm playing with ASP.NET 5 RC1 and am using Identity 3.0 to provide authentication using usernames/passwords, using the built in ASPNET schema and EF7.

    For testing purposes, I'm trying to seed the database with roles and users, using a static method called from Startup.cs. The following code works for adding roles, but not for users. (I couldn't find any sample code for using the new RoleManager and UserManager classes in Identity 3). Am I using the correct method call to UserManager here?

            if (!context.Roles.Any(r => r.Name == "Admin"))
            {
                var store = new RoleStore<IdentityRole>(context);
                var manager = new RoleManager<IdentityRole>(store, null, null, null, null, null);
                var role = new IdentityRole { Name = "Admin" };
                manager.CreateAsync(role);
            }
    
            if (!context.Roles.Any(r => r.Name == "User"))
            {
                var store = new RoleStore<IdentityRole>(context);
                var manager = new RoleManager<IdentityRole>(store, null, null, null, null, null);
                var role = new IdentityRole { Name = "User" };
                manager.CreateAsync(role);
            }
    
            if (!context.Users.Any(u => u.UserName == "darin"))
            {
                var store = new UserStore<ApplicationUser>(context);
                var manager = new UserManager<ApplicationUser>(store, null, null, null, null, null, null, null, null, null);
                var user = new ApplicationUser { UserName = "darin", Email = "dherle@gmail.com" };
                manager.CreateAsync(user, "admin");
                manager.AddToRoleAsync(user, "Admin");
            }
    
            if (!context.Users.Any(u => u.UserName == "chris"))
            {
                var store = new UserStore<ApplicationUser>(context);
                var manager = new UserManager<ApplicationUser>(store, null, null, null, null, null, null, null, null, null);
                var user = new ApplicationUser { UserName = "chris", Email = "dan@gmail.com" };
                manager.CreateAsync(user, "chris");
                manager.AddToRoleAsync(user, "User");
            }
    Wednesday, December 16, 2015 1:32 AM

All replies

  • User-2057865890 posted

    Hi,deherle

    Welcome to asp.net forum.

    deherle

    (I couldn't find any sample code for using the new RoleManager and UserManager classes in Identity 3

    ASP.NET Identity is the new membership system for building ASP.NET web applications.

    ASP.NET Identity allows you to add login features to your application and makes it easy to customize data about the logged in user.

    The ASP.NET 5 security tutorials are at http://docs.asp.net/en/latest/security/index.html.

    Using Identity in ASP.NET applications

    Identity Sample

    Best regards,

    Chris Zhao

    Thursday, December 17, 2015 6:43 AM
  • User1474298650 posted

    Hi Chris,

    Thanks for the link!

    I've read through this and there isn't sufficient detail to help with my question.

    Taking a step back, I realized that ideally I'd get a UserManager/RoleManager instance in Startup.cs or on the first HTTP request.  Is there any sample code for getting those in either of these situations with Identity 3 and MVC5?  There are examples for Identity 2 and MVC 4, but nothing for the latest releases.

    Thanks.

    Darin

    Thursday, December 17, 2015 6:09 PM