public virtual async Task <IdentityDataSeedResult> SeedAsync(
            string adminEmail,
            string adminPassword,
            Guid?tenantId = null)
        {
            Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
            Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));

            var result = new IdentityDataSeedResult();

            //"admin" user
            const string adminUserName = "******";
            var          adminUser     = await UserRepository.FindByNormalizedUserNameAsync(
                LookupNormalizer.NormalizeName(adminUserName)
                );

            if (adminUser != null)
            {
                return(result);
            }

            adminUser = new IdentityUser(
                GuidGenerator.Create(),
                Guid.Empty,
                adminUserName,
                adminEmail,
                tenantId
                )
            {
                Name = adminUserName
            };

            (await UserManager.CreateAsync(adminUser, adminPassword)).CheckErrors();
            result.CreatedAdminUser = true;

            //"admin" role
            const string adminRoleName = "admin";
            var          adminRole     = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName));

            if (adminRole == null)
            {
                adminRole = new IdentityRole(
                    GuidGenerator.Create(),
                    adminRoleName,
                    tenantId
                    )
                {
                    IsStatic = true,
                    IsPublic = true
                };

                (await RoleManager.CreateAsync(adminRole)).CheckErrors();
                result.CreatedAdminRole = true;
            }

            (await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors();

            return(result);
        }
Beispiel #2
0
        public virtual async Task <IdentityDataSeedResult> SeedAsync(
            string adminUserPassword,
            Guid?tenantId = null)
        {
            var result = new IdentityDataSeedResult();

            const string adminUserName = "******";
            const string adminRoleName = "admin";

            //"admin" user
            var adminUser = await _userRepository.FindByNormalizedUserNameAsync(_lookupNormalizer.Normalize(adminUserName));

            if (adminUser != null)
            {
                return(result);
            }

            adminUser      = new IdentityUser(_guidGenerator.Create(), adminUserName, "*****@*****.**", tenantId);
            adminUser.Name = adminUserName;
            CheckIdentityErrors(await _userManager.CreateAsync(adminUser, adminUserPassword));
            result.CreatedAdminUser = true;

            //"admin" role
            var adminRole = await _roleRepository.FindByNormalizedNameAsync(_lookupNormalizer.Normalize(adminRoleName));

            if (adminRole == null)
            {
                adminRole = new IdentityRole(_guidGenerator.Create(), adminRoleName, tenantId);

                adminRole.IsStatic = true;
                adminRole.IsPublic = true;

                CheckIdentityErrors(await _roleManager.CreateAsync(adminRole));
                result.CreatedAdminRole = true;
            }

            CheckIdentityErrors(await _userManager.AddToRoleAsync(adminUser, adminRoleName));

            return(result);
        }