コード例 #1
0
        public async Task<IActionResult> Create(RoleViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                    throw new Exception("Role is not valid");

                var role = new ApplicationRole { Name = model.Name, Description = model.Description};
                var result = await _applicationRoleManager.CreateAsync(role);

                if (!result.Succeeded)
                {
                    throw new Exception("Can't create the role!");
                }
                
                var msg = CreateMessage(ControllerActionType.Create, "role", model.Name);
                return RedirectToAction("Index");
                
            }
            catch (Exception ex)
            {
                var msg = CreateMessage(ControllerActionType.Create, "role", model.Name, ex);
                return View(model);
            }
        }
コード例 #2
0
 private async Task<bool> CreateRoles() 
 {
     if(_context.Roles == null || _context.Roles.Count() == 0)
     {
         var role = new ApplicationRole 
         { 
             Name = "Administrator", 
             Description = "Administrator: administration of all the content."
         };
         var result = await _roleManager.CreateAsync(role);
         
         if(!result.Succeeded)
             return false;
             
         role = new ApplicationRole 
         { 
             Name = "LibrariesPrincipal", 
             Description = "Libraries principal: principal for all the libraries"
         };
         result = await _roleManager.CreateAsync(role);
         
         if(!result.Succeeded)
             return false;
             
         role = new ApplicationRole 
         { 
             Name = "LibraryCoordinator", 
             Description = "Library coordinator: coordinator for a specific library"
         };
         result = await _roleManager.CreateAsync(role);
         
         if(!result.Succeeded)
             return false;
             
         role = new ApplicationRole 
         { 
             Name = "LibraryCoordinator", 
             Description = "Library coordinator: coordinator for a specific library"
         };
         result = await _roleManager.CreateAsync(role);
         
         if(!result.Succeeded)
             return false;
             
             
     }
     return true;
 }