예제 #1
0
        private static void SeedAdmin(string userName, string password, IdentityDbContext context, UserManager <AppUserEntity> userManager,
                                      RoleManager <RoleEntity> roleManager, ILogger logger)
        {
            AppUserEntity admin = new AppUserEntity()
            {
                UserName       = userName,
                EmailConfirmed = true,
                Enabled        = true,
            };

            {
                Task <IdentityResult> createAdmin = userManager.CreateAsync(admin, password);
                Task.WaitAll(createAdmin);

                IdentityResult createAdminResult = createAdmin.Result;
                if (!createAdminResult.Succeeded)
                {
                    throw new Exception($"Faild to add admin. {string.Join(" ", createAdminResult.Errors.Select(x => x.Description))}");
                }
            }

            RoleEntity role;

            role = new RoleEntity(RoleConstats.IDENTITY_MANAGMENT_ROLE, "Has access to IdentityServerManagment");

            {
                var createRole = roleManager.CreateAsync(role);
                Task.WaitAll(createRole);

                IdentityResult createRoleResult = createRole.Result;
                if (!createRoleResult.Succeeded)
                {
                    throw new Exception($"Faild to add admin role. {string.Join(" ", createRoleResult.Errors.Select(x => x.Description))}");
                }
            }

            UserRoleEntity userRole = new UserRoleEntity(admin.Id, role.Id);

            context.UserRoles.Add(userRole);

            int changes = context.SaveChanges();

            if (changes <= 0)
            {
                throw new Exception($"Faild to link admin to admin role");
            }

            logger.LogInformation($"Admin was seeded");
        }
예제 #2
0
        /// <summary>
        /// Creates a new database without migrations
        /// </summary>
        /// <param name="app"></param>
        public static IApplicationBuilder CreateIdentityDatabase(this IApplicationBuilder app)
        {
            using (var scope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                IdentityDbContext context = scope.ServiceProvider.GetService <IdentityDbContext>();

                ILoggerFactory loggerFactory = scope.ServiceProvider.GetService <ILoggerFactory>();
                ILogger        logger        = loggerFactory.CreateLogger(typeof(IdentityDbContextSeeder));

                bool result = context.Database.EnsureCreated();
                if (result)
                {
                    logger.LogInformation($"IdentityDatabase created");
                }
            }

            return(app);
        }
예제 #3
0
 public IdentityUIBaseDAO(IdentityDbContext dbContext) : base(dbContext)
 {
 }
예제 #4
0
 public AuditBaseDAO(IdentityDbContext dbContext) : base(dbContext)
 {
 }