F1 - Autentcacion - Completa

This commit is contained in:
2026-08-10 15:54:42 -03:00
commit dd7f5b3bf6
245 changed files with 81985 additions and 0 deletions
+86
View File
@@ -0,0 +1,86 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using System.Linq;
namespace vita_asistente.Controllers
{
[Authorize(Roles = "Admin")]
public class RolesController : Controller
{
private readonly RoleManager<IdentityRole> _roleManager;
public RolesController(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
// GET: Roles
public async Task<IActionResult> Index()
{
var roles = await _roleManager.Roles.ToListAsync();
return View(roles);
}
// GET: Roles/Create
public IActionResult Create()
{
return View();
}
// POST: Roles/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string roleName)
{
if (string.IsNullOrEmpty(roleName))
{
ModelState.AddModelError("", "El nombre del rol no puede estar vacío.");
return View();
}
var roleExists = await _roleManager.RoleExistsAsync(roleName);
if (!roleExists)
{
var result = await _roleManager.CreateAsync(new IdentityRole(roleName));
if (result.Succeeded)
{
return RedirectToAction(nameof(Index));
}
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
else
{
ModelState.AddModelError("", "El rol ya existe.");
}
return View();
}
// GET: Roles/Delete/5
public async Task<IActionResult> Delete(string id)
{
if (id == null) return NotFound();
var rol = await _roleManager.FindByIdAsync(id);
if (rol == null) return NotFound();
return View(rol);
}
// POST: Roles/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id)
{
var rol = await _roleManager.FindByIdAsync(id);
if (rol != null)
{
await _roleManager.DeleteAsync(rol);
}
return RedirectToAction(nameof(Index));
}
}
}