Example #1
0
 public static void CreateUsersForReport(ApplicationDbContext context, string usersstring)
 {
     var userManager = new ApplicationUserManager(new UserStore<User>(context));
     string[][] userarray = usersstring.Split(',').Select(str => str.Split(' ')).ToArray();
     foreach (var strar in userarray)
     {
         User usersu = userManager.FindByName(strar[0] + " " + strar[1]);
         if (usersu == null)
         {
             usersu = new User
             {
                 UserName = strar[0] + " " + strar[1],
                 FirstName = strar[1],
                 LastName = strar[0],
                 SecurityStamp = Guid.NewGuid().ToString(),
                 LastLoginTime = DateTime.UtcNow,
                 RegistrationDate = DateTime.UtcNow,
                 PasswordHash =
                     userManager.PasswordHasher.HashPassword("12345")
             };
             IdentityRole role = context.Roles.FirstOrDefault(r => string.Equals(r.Name, "Employee"));
             if (role != null) usersu.Roles.Add(new IdentityUserRole {RoleId = role.Id, UserId = usersu.Id});
             context.Entry(usersu).State = EntityState.Added;
         }
     }
     context.SaveChanges();
 }
Example #2
0
        public static void CreateUsersForReport(ApplicationDbContext context, string usersstring)
        {
            var userManager = new ApplicationUserManager(new UserStore <User>(context));

            string[][] userarray = usersstring.Split(',').Select(str => str.Split(' ')).ToArray();
            foreach (var strar in userarray)
            {
                User usersu = userManager.FindByName(strar[0] + " " + strar[1]);
                if (usersu == null)
                {
                    usersu = new User
                    {
                        UserName         = strar[0] + " " + strar[1],
                        FirstName        = strar[1],
                        LastName         = strar[0],
                        SecurityStamp    = Guid.NewGuid().ToString(),
                        LastLoginTime    = DateTime.UtcNow,
                        RegistrationDate = DateTime.UtcNow,
                        PasswordHash     =
                            userManager.PasswordHasher.HashPassword("12345")
                    };
                    IdentityRole role = context.Roles.FirstOrDefault(r => string.Equals(r.Name, "Employee"));
                    if (role != null)
                    {
                        usersu.Roles.Add(new IdentityUserRole {
                            RoleId = role.Id, UserId = usersu.Id
                        });
                    }
                    context.Entry(usersu).State = EntityState.Added;
                }
            }
            context.SaveChanges();
        }