F1 - Autentcacion - Completa
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using vita_asistente.Models;
|
||||
using vita_asistente.ViewModels;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace vita_asistente.Controllers
|
||||
{
|
||||
public class AccountController : Controller
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
|
||||
public AccountController(UserManager<ApplicationUser> userManager,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
// GET: /Account/Register
|
||||
[HttpGet]
|
||||
public IActionResult Register()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: /Account/Register
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Register(RegisterViewModel model)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
// Crear el usuario con los datos del formulario
|
||||
var user = new ApplicationUser
|
||||
{
|
||||
UserName = model.Email, // Usamos Email como nombre de usuario
|
||||
Email = model.Email,
|
||||
Nombre = model.Nombre,
|
||||
Apellido = model.Apellido,
|
||||
Documento = model.Documento,
|
||||
Direccion = model.Direccion,
|
||||
RolPrincipal = "Usuario" // valor por defecto, luego puedes asignar roles
|
||||
};
|
||||
|
||||
var result = await _userManager.CreateAsync(user, model.Password);
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
// Asignar rol "Usuario" por defecto (si el rol existe)
|
||||
// Si no existe, lo creamos o lo ignoramos. Lo veremos después.
|
||||
// Por ahora, solo iniciamos sesión.
|
||||
await _signInManager.SignInAsync(user, isPersistent: false);
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
ModelState.AddModelError(string.Empty, error.Description);
|
||||
}
|
||||
}
|
||||
|
||||
// Si llegamos aquí, algo falló, devolver la vista con errores
|
||||
return View(model);
|
||||
}
|
||||
|
||||
// GET: /Account/Login
|
||||
[HttpGet]
|
||||
public IActionResult Login(string returnUrl = null)
|
||||
{
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: /Account/Login
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
|
||||
{
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
var result = await _signInManager.PasswordSignInAsync(
|
||||
model.Email,
|
||||
model.Password,
|
||||
model.RememberMe,
|
||||
lockoutOnFailure: false);
|
||||
|
||||
if (result.Succeeded)
|
||||
{
|
||||
return RedirectToLocal(returnUrl);
|
||||
}
|
||||
if (result.RequiresTwoFactor)
|
||||
{
|
||||
// Manejar 2FA si lo implementas después
|
||||
return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe });
|
||||
}
|
||||
if (result.IsLockedOut)
|
||||
{
|
||||
return RedirectToAction(nameof(Lockout));
|
||||
}
|
||||
else
|
||||
{
|
||||
ModelState.AddModelError(string.Empty, "Intento de inicio de sesión inválido.");
|
||||
return View(model);
|
||||
}
|
||||
}
|
||||
|
||||
// Si llegamos aquí, algo falló
|
||||
return View(model);
|
||||
}
|
||||
|
||||
// POST: /Account/Logout
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
|
||||
// Acciones auxiliares para redirección, 2FA, bloqueo, etc. (opcionales)
|
||||
private IActionResult RedirectToLocal(string returnUrl)
|
||||
{
|
||||
if (Url.IsLocalUrl(returnUrl))
|
||||
{
|
||||
return Redirect(returnUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
}
|
||||
|
||||
// GET: /Account/Lockout
|
||||
[HttpGet]
|
||||
public IActionResult Lockout()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
// GET: /Account/LoginWith2fa (opcional)
|
||||
[HttpGet]
|
||||
public IActionResult LoginWith2fa(bool rememberMe, string returnUrl = null)
|
||||
{
|
||||
// Implementar si se usa 2FA
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user