Exemplo n.º 1
0
        public static async Task <SignInResult> PasswordSignInAsync(this SignInManager <ApplicationUser> sm, string userEmail, string password, bool isPersistent, bool lockoutOnFailure)
        {
            var user = await UserManagerExtensions.FindByEmailAsync(sm.UserManager, userEmail);

            if (user == null)
            {
                return(SignInResult.Failed);
            }
            return(await sm.PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure));
        }
Exemplo n.º 2
0
        public static void InitializeDatabase(this IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                var applicationDbContext    = serviceScope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                var persistedGrantDbContext = serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>();

                applicationDbContext.Database.Migrate();
                persistedGrantDbContext.Database.Migrate();
                ////
                //var configContext = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
                //if (!configContext.Clients.Any())
                //{
                //    foreach (var client in Config.GetClients())
                //    {
                //        configContext.Clients.Add(client.ToEntity());
                //    }
                //    configContext.SaveChanges();
                //}
                //if (!configContext.IdentityResources.Any())
                //{
                //    foreach (var resource in Config.GetIdentityResources())
                //    {
                //        configContext.IdentityResources.Add(resource.ToEntity());
                //    }
                //    configContext.SaveChanges();
                //}
                ////

                //建製角色
                Task.Run(async() =>
                {
                    await BulderRolesAsync(serviceScope);
                }).GetAwaiter().GetResult();


                var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
                Task.Run(async() =>
                {
                    var admin = await UserManagerExtensions.FindByEmailAsync(userManager, "*****@*****.**");
                    if (admin == null)
                    {
                        //建立使用者
                        var applicationUser = new ApplicationUser
                        {
                            UserName = "******",
                            Email    = "*****@*****.**"
                        };
                        var createUser = await userManager.CreateAsync(applicationUser, "1234");
                        if (createUser.Succeeded)
                        {
                            //here we tie the new user to the "Admin" role
                            await userManager.AddToRoleAsync(applicationUser, "Admin");
                        }
                    }
                    //
                    //var user = await UserManagerExtensions.FindByEmailAsync(userManager, "*****@*****.**");
                    //if (user == null)
                    //{
                    //    //建立使用者
                    //    var applicationUser = new ApplicationUser
                    //    {
                    //        UserName = "******",
                    //        Email = "*****@*****.**"
                    //    };
                    //    var createUser = await userManager.CreateAsync(applicationUser, "1234");
                    //    if (createUser.Succeeded)
                    //    {
                    //        //建立角色
                    //        await userManager.AddToRoleAsync(applicationUser, "General");
                    //        await userManager.AddToRoleAsync(applicationUser, "ShopManager");
                    //    }
                    //}
                    //測試相同的email delflag不同
                    var user = await UserManagerExtensions.FindByEmailAsync(userManager, "*****@*****.**");
                    if (user == null)
                    {
                        //建立使用者
                        var applicationUser = new ApplicationUser
                        {
                            UserName = "******",
                            Email    = "*****@*****.**"
                        };
                        var createUser = await userManager.CreateAsync(applicationUser, "1234");
                        if (createUser.Succeeded)
                        {
                            //建立角色
                            await userManager.AddToRoleAsync(applicationUser, "General");
                            await userManager.AddToRoleAsync(applicationUser, "ShopManager");
                        }
                    }
                }).GetAwaiter().GetResult();


                //if (configContext.ApiResources.Any()) return;

                //foreach (var resource in Config.GetApiResources())
                //{
                //    configContext.ApiResources.Add(resource.ToEntity());
                //}

                //configContext.SaveChanges();
            }
        }