示例#1
0
        public async void DataInitialize()
        {
            TravelBlogDbContext context = new TravelBlogDbContext();

            string[] roles     = new string[] { "Admin" };
            var      roleStore = new RoleStore <IdentityRole>(context);

            foreach (string role in roles)
            {
                if (!context.Roles.Any(r => r.Name == role))
                {
                    IdentityRole newRole = new IdentityRole(role);
                    newRole.NormalizedName = "ADMIN";
                    await roleStore.CreateAsync(newRole);
                }
            }
            var userStore = new UserStore <ApplicationUser>(context);

            if (!context.Users.Any(u => u.UserName == "*****@*****.**"))
            {
                var user = new ApplicationUser {
                    UserName = "******"
                };
                var password = new PasswordHasher <ApplicationUser>();
                var hashed   = password.HashPassword(user, "Test1234!");
                user.PasswordHash = hashed;

                var result = await userStore.CreateAsync(user);

                await userStore.AddToRoleAsync(user, "ADMIN");
            }
        }
 public HomeController(UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, TravelBlogDbContext db)
 {
     _db            = db;
     _userManager   = userManager;
     _signInManager = signInManager;
 }