示例#1
0
        private async Task CreateAdminUser(IServiceProvider serviceProvider)
        {
            var UserManager    = serviceProvider.GetRequiredService <UserManager <AppUser> >();
            var UserRepository = serviceProvider.GetRequiredService <IUserRepository>();
            var UnitOfWork     = serviceProvider.GetRequiredService <IUnitOfWork>();
            // Add admin user if not already exist
            var user = await UserManager.FindByEmailAsync(Constants.Strings.AdminUser.Email);

            if (user == null)
            {
                var userIdentity = new AppUser();

                userIdentity.UserName  = Constants.Strings.AdminUser.Email;
                userIdentity.FirstName = Constants.Strings.AdminUser.FirstName;
                userIdentity.LastName  = Constants.Strings.AdminUser.LastName;
                userIdentity.Email     = Constants.Strings.AdminUser.Email;

                var result = await UserManager.CreateAsync(userIdentity, Configuration["adminpwd"]);

                if (result.Succeeded)
                {
                    var adminClaims = RoleClaimSetup.RoleClaimSetupAdmin();
                    await UserManager.AddToRoleAsync(userIdentity, adminClaims.Role.Name);

                    await UnitOfWork.CompleteAsync();
                }
            }
        }
示例#2
0
        private async Task CreateUserRoles(IServiceProvider serviceProvider)
        {
            var RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();

            bool x = await RoleManager.RoleExistsAsync(Constants.Strings.JwtClaims.AdminUser);

            if (!x)
            {
                var claims = RoleClaimSetup.RoleClaimSetupAdmin();
                await RoleManager.CreateAsync(claims.Role);

                foreach (var claim in claims.Claims)
                {
                    await RoleManager.AddClaimAsync(claims.Role, claim);
                }
            }

            //Role : Designer Survey
            x = await RoleManager.RoleExistsAsync(Constants.Strings.JwtClaims.DesignerSurveyUser);

            if (!x)
            {
                var claims = RoleClaimSetup.RoleClaimSetupDesignerSurvey();
                await RoleManager.CreateAsync(claims.Role);

                foreach (var claim in claims.Claims)
                {
                    await RoleManager.AddClaimAsync(claims.Role, claim);
                }
            }

            x = await RoleManager.RoleExistsAsync(Constants.Strings.JwtClaims.DesignerDrawingUser);

            if (!x)
            {
                var claims = RoleClaimSetup.RoleClaimSetupDesignerDrawer();
                await RoleManager.CreateAsync(claims.Role);

                foreach (var claim in claims.Claims)
                {
                    await RoleManager.AddClaimAsync(claims.Role, claim);
                }
            }
        }