示例#1
0
        public static void InitializeUsers(ApplicationDbContext context)
        {
            const string email = "*****@*****.**";

            var admin = new ApplicationUser()
            {
                FirstName            = "Admin",
                LastName             = "User",
                Email                = email.ToLower(),
                NormalizedEmail      = email?.ToUpper(),
                PhoneNumber          = "+19717067846",
                PhoneNumberConfirmed = true,
                SocialSecurityNumber = "000000000",
                DateOfBirth          = Formatting.AsMilDate(new DateTime(2001, 11, 01)),
                UserName             = email.ToLower(),
                NormalizedUserName   = email?.ToUpper(),
                SecurityStamp        = Guid.NewGuid().ToString("D"),
                FullName             = "User, Admin",
                AssignedUnit         = "ADMIN"
            };

            var password = new PasswordHasher <ApplicationUser>();
            var hashed   = password.HashPassword(admin, PasswordProtocol.CalculateHash("Secret123$", admin.Salt));

            admin.PasswordHash = hashed;

            var userManager = context.GetService <UserManager <ApplicationUser> >();

            userManager.CreateAsync(admin).Wait();
            userManager.AddToRoleAsync(admin, "ADMIN").Wait();

            context.SaveChanges();

            var qc = new ApplicationUser()
            {
                Rank                 = "SPC",
                FirstName            = "Aaron",
                MiddleName           = "Lloyd",
                LastName             = "Day",
                Email                = "*****@*****.**",
                NormalizedEmail      = "*****@*****.**",
                PhoneNumber          = "+19717067846",
                PhoneNumberConfirmed = true,
                SocialSecurityNumber = "000005327",
                DateOfBirth          = Formatting.AsMilDate(new DateTime(1983, 07, 06)),
                UserName             = "******",
                NormalizedUserName   = "******",
                SecurityStamp        = Guid.NewGuid().ToString("D"),
                FullName             = "SPC Day, Aaron",
                AssignedUnit         = "WFJ5B0"
            };

            hashed          = password.HashPassword(qc, PasswordProtocol.CalculateHash("Secret123$", qc.Salt));
            qc.PasswordHash = hashed;

            userManager.CreateAsync(qc).Wait();
            userManager.AddToRoleAsync(qc, "QC").Wait();
            userManager.AddToRoleAsync(qc, "TI").Wait();
            userManager.AddToRoleAsync(qc, "CE").Wait();

            context.SaveChanges();
        }