Exemple #1
0
        protected void Application_Start()
        {
            using (SaglikContext ent = new SaglikContext())
            {
                ent.Database.CreateIfNotExists();
            }
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //Role Tanımlama Adımları
            SaglikContext db = new SaglikContext();
            RoleStore <AplicationRole>   roleStore   = new RoleStore <AplicationRole>(db);
            RoleManager <AplicationRole> roleManager = new RoleManager <AplicationRole>(roleStore);

            if (!roleManager.RoleExists("Admin"))
            {
                AplicationRole adminRole = new AplicationRole("Admin", "Sistem Yöneticisi");
                roleManager.Create(adminRole);
            }
            if (!roleManager.RoleExists("User"))
            {
                AplicationRole UserRole = new AplicationRole("User", "Sistem Kullanıcısı");
                roleManager.Create(UserRole);
            }
        }
Exemple #2
0
        public ActionResult CrearRoles(AplicationRole role)
        {
            var rolemanager = new CustomRolePrivider();

            rolemanager.CreateRole(role.Name);
            return(PartialView());
        }
Exemple #3
0
        public async Task <ActionResult> delete(string id)
        {
            if (ModelState.IsValid)
            {
                ViewBag.returnUrl = "/RoleUsers/Index";
                AplicationRole role = await roleManager.FindByIdAsync(id);

                if (role != null)
                {
                    var result = await roleManager.DeleteAsync(role);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(View("Error", result.Errors));
                    }
                }
                else
                {
                    return(View("Error", new string[] { "Not Found This Role" }));
                }
            }
            return(View("Error", new string[] { "Invalidate Role" }));
        }
Exemple #4
0
        public async Task <ActionResult> create([Required] string name)
        {
            if (ModelState.IsValid)
            {
                AplicationRole roleModel = new AplicationRole(name);
                var            result    = await roleManager.CreateAsync(roleModel);

                if (!result.Succeeded)
                {
                    foreach (var item in result.Errors)
                    {
                        ModelState.AddModelError("", item);
                    }
                    return(View(name));
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
            else
            {
                ModelState.AddModelError("", "无效Name");
                return(View(""));
            }
        }
Exemple #5
0
        public async Task <ActionResult> Create(RoleViewModel roleViewModel)
        {
            if (ModelState.IsValid)
            {
                var role = new AplicationRole(roleViewModel.Name);
                role.descripcion = roleViewModel.descripcion;
                var roleresult = await RoleManager.CreateAsync(role);

                if (!roleresult.Succeeded)
                {
                    ModelState.AddModelError("", roleresult.Errors.First());
                    return(View());
                }
                return(RedirectToAction("Index"));
            }
            return(View());
        }
Exemple #6
0
        protected override void Seed(_123TribeFrameworker.Models.ApplicationDbContext context)
        {
            //初始化角色 要有一个administrator角色
            //初始化用户 要有一个admin用户 密码 hai123456
            //给admin配置administrator角色
            //给administrator配置所有菜单权限
            ApplicationRoleManager roleManager = new ApplicationRoleManager(new RoleStore <AplicationRole>(context));
            ApplicationUserManager userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(context));
            string          roleName           = "administrator";
            ApplicationUser user = new ApplicationUser()
            {
                UserName = "******"
            };

            if (!roleManager.RoleExists(roleName))
            {
                roleManager.Create(new AplicationRole(roleName));
            }
            if (userManager.FindByName("admin") == null)
            {
                userManager.Create(user, "hai123456");
                user = userManager.FindByName("admin");
            }
            if (!userManager.IsInRole(user.Id, roleName))
            {
                userManager.AddToRole(user.Id, roleName);
            }

            RoleMenuLayerImpl layer = new RoleMenuLayerImpl();
            AplicationRole    role  = roleManager.FindByName(roleName);
            var firstDirs           = layer.searchRoleMenusNotInRoleId(role.Id, DirLevel.FirstLevel);
            var secondDirs          = layer.searchRoleMenusNotInRoleId(role.Id, DirLevel.SecondLevel);

            if (firstDirs != null && firstDirs.Count() > 0)
            {
                var resultF = layer.addRoleMenuRangeAsync(role.Id, DirLevel.FirstLevel, firstDirs.Select(x => x.menuId).ToArray());
            }
            if (secondDirs != null && secondDirs.Count() > 0)
            {
                var resultS = layer.addRoleMenuRangeAsync(role.Id, DirLevel.SecondLevel, secondDirs.Select(x => x.menuId).ToArray());
            }
        }
Exemple #7
0
        public async Task <ActionResult> edit(string id)
        {
            if (ModelState.IsValid)
            {
                AplicationRole role = await roleManager.FindByIdAsync(id);

                string[] ids = role.Users.Select(x => x.UserId).ToArray();
                IEnumerable <ApplicationUser> members    = userManager.Users.Where(x => ids.Any(y => y == x.Id));
                IEnumerable <ApplicationUser> nonMembers = userManager.Users.Except(members);
                return(View(new RoleEditModel
                {
                    role = role,
                    members = members.ToList(),
                    nonMembers = nonMembers.ToList()
                }));
            }
            else
            {
                ViewBag.returnUrl = "/RoleUsers/Index";
                return(View("Error", new string[] { "Invalidate Role" }));
            }
        }
Exemple #8
0
        public static async Task Initialize(ApplicationDbContext context,
                                            UserManager <AplicationUser> userManager,
                                            RoleManager <AplicationRole> roleManager)
        {
            context.Database.EnsureCreated();
            string[] roleNames = { "admin", "estudiante", "docenete" };
            foreach (var roleName in roleNames)
            {
                if (!roleManager.RoleExistsAsync(roleName).Result)
                {
                    AplicationRole role = new AplicationRole
                    {
                        Name           = roleName,
                        NormalizedName = roleName
                    };
                    roleManager.CreateAsync(role).Wait();
                }
            }

            if (await userManager.FindByEmailAsync("*****@*****.**") == null)
            {
                AplicationUser user = new AplicationUser
                {
                    UserName       = "******",
                    Email          = "*****@*****.**",
                    EmailConfirmed = true,
                    LockoutEnabled = false
                };

                IdentityResult result = userManager.CreateAsync(user, "Admin1234$").Result;

                if (result.Succeeded)
                {
                    userManager.AddToRoleAsync(user, "admin").Wait();
                }
            }
        }