Ejemplo n.º 1
0
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                var logger        = loggerFactory.CreateLogger("app");
                try
                {
                    var userManager = services.GetRequiredService <UserManager <IdentityUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();

                    await DefaultRoles.SeedAsync(userManager, roleManager);

                    await DefaultUsers.SeedBasicUserAsync(userManager, roleManager);

                    await DefaultUsers.SeedSuperAdminAsync(userManager, roleManager);

                    logger.LogInformation("Finished Seeding Default Data");
                    logger.LogInformation("Application Starting");
                }
                catch (Exception ex)
                {
                    logger.LogWarning(ex, "An error occurred seeding the DB");
                }
            }
            host.Run();
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Update(string id, ManageUserRolesViewModel manageUserRolesVM)
        {
            var user = await _userManager.FindByIdAsync(id);

            var roles = await _userManager.GetRolesAsync(user);

            var result = await _userManager.RemoveFromRolesAsync(user, roles);

            result = await _userManager.AddToRolesAsync(user, manageUserRolesVM.UserRoles.Where( x => x.Selected).Select(y => y.RoleName));

            //reapply roles and permissions for superAdmin in case any other admin tries to change its permissions
            //this can be changed or deleted depending on the business logic
            var currentUser = await _userManager.GetUserAsync(User);

            await _singInManager.RefreshSignInAsync(currentUser);

            await DefaultUsers.SeedSuperAdminAsync(_userManager, _roleManager);

            return(RedirectToAction(nameof(Index), new { userId = id }));
        }