Example #1
0
        private static async System.Threading.Tasks.Task CreateAdminAccount(UserManager <AspNetUser> userManager, RoleManager <AspNetRole> roleManager)
        {
            string username = "******";
            string email    = "*****@*****.**";
            string password = "******";

            if (await userManager.FindByNameAsync(username) == null)
            {
                AspNetUser user = new AspNetUser
                {
                    UserName = username,
                    FullName = username,
                    Email    = email
                };

                IdentityResult result = await userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    IdentityRepository indentityRepo = new IdentityRepository(context, userManager);

                    AspNetUser aspNetUser = await userManager.FindByNameAsync(username);

                    AspNetGroup aspNetGroup = indentityRepo.GetAspNetGroup("Administrator");

                    AspNetUserGroup aspNetUserGroup = new AspNetUserGroup();
                    aspNetUserGroup.GroupId = aspNetGroup.GroupId;
                    aspNetUserGroup.UserId  = aspNetUser.Id;
                    aspNetUserGroup.Active  = true;

                    indentityRepo.AddUserToGroup(aspNetUserGroup);
                }
            }
        }