示例#1
0
        private static async Task CreateUserRoles(IServiceProvider ServiceProvider)
        {
            NewsContext a = (NewsContext)ServiceProvider.GetService(typeof(DbContext));

            a.Initialize();

            RoleManager <IdentityRole> RoleManager = ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();
            UserManager <User>         UserManager = ServiceProvider.GetRequiredService <UserManager <User> >();

            if (await RoleManager.FindByNameAsync(RolesNames.Admin) == null)
            {
                await RoleManager.CreateAsync(new IdentityRole(RolesNames.Admin));
            }
            if (await RoleManager.FindByNameAsync(RolesNames.User) == null)
            {
                await RoleManager.CreateAsync(new IdentityRole(RolesNames.User));
            }
            string AdminName     = "InarBelkin";
            string AdminPassword = "******";

            if (await UserManager.FindByNameAsync(AdminName) == null)
            {
                User Admin = new User
                {
                    UserName = AdminName
                };

                //Secure_Password
                IdentityResult Result = await UserManager.CreateAsync(Admin, AdminPassword);

                if (Result.Succeeded)
                {
                    await UserManager.AddToRoleAsync(Admin, RolesNames.Admin);
                }
            }

            string UserName     = "******";
            string UserPassword = "******";

            if (await UserManager.FindByNameAsync(UserName) == null)
            {
                User Quest = new User
                {
                    UserName = UserName
                };

                //qwerty
                IdentityResult Result = await UserManager.CreateAsync(Quest, UserPassword);

                if (Result.Succeeded)
                {
                    await UserManager.AddToRoleAsync(Quest, RolesNames.User);
                }
            }
        }