public async Task <IActionResult> CrearRol(CrearRolViewModel model) { if (ModelState.IsValid) { // We just need to specify a unique role name to create a new role var identityRole = new ApplicationRole { Name = model.NombreRol, Area = model.Area, CreatedBy = HttpContext.User.Identity.Name, CreationDate = DateTime.Now, CreationIp = HttpContext.Connection.RemoteIpAddress.ToString(), LastUpdatedBy = HttpContext.User.Identity.Name, LastUpdatedDate = DateTime.Now, LastUpdatedIp = HttpContext.Connection.RemoteIpAddress.ToString(), Habilitado = true }; // Saves the role in the underlying AspNetRoles table IdentityResult result = await roleManager.CreateAsync(identityRole); if (result.Succeeded) { return(RedirectToAction("Roles", "Administracion")); } foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } } return(View(model)); }
public ActionResult Crear(CrearRolViewModel model) { var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext())); if (roleManager.RoleExists(model.Nombre.Trim())) { return(Json(new MensajeRespuestaViewModel { Titulo = "Crear Rol", Mensaje = "El Rol ya Existe", Estado = false }, JsonRequestBehavior.AllowGet)); } var result = roleManager.Create(new IdentityRole { Name = model.Nombre.Trim() }); if (result.Succeeded) { using (var context = new CripcoEntities()) { context.AspNetRoles.FirstOrDefault(x => x.Name == model.Nombre.Trim()).Activo = true; context.SaveChanges(); } } return(Json(new MensajeRespuestaViewModel { Titulo = "Crear Rol", Mensaje = result.Succeeded ? "Se creo Satisfactoriamente" : "Error al crear el Rol: " + result.Errors.FirstOrDefault(), Estado = result.Succeeded }, JsonRequestBehavior.AllowGet)); }
public ActionResult Editar(CrearRolViewModel model) { using (var context = new CripcoEntities()) { var rol = context.AspNetRoles.Find(model.Id); rol.Name = model.Nombre; rol.Activo = model.Estado; context.Entry(rol).State = System.Data.Entity.EntityState.Modified; var result = context.SaveChanges() > 0; return(Json(new MensajeRespuestaViewModel { Titulo = "Editar Rol", Mensaje = result ? "Se edito Satisfactoriamente" : "Error al editar ", Estado = result }, JsonRequestBehavior.AllowGet)); } }
public async Task <IActionResult> CrearRol(CrearRolViewModel model) { if (ModelState.IsValid) { IdentityRole identityrole = new IdentityRole { Name = model.NombreRol }; IdentityResult result = await gestionRoles.CreateAsync(identityrole); if (result.Succeeded) { return(RedirectToAction("index", "home")); } foreach (IdentityError error in result.Errors) { ModelState.AddModelError("", error.Description); } } return(View(model)); }