/
tye4104
/
asp-net
Обзор
Документация
Войти
/
tye4104
/
asp-net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
Controllers/AccountController.cs
155 строк
4 KB
tye4104
Initial commit
08 июн 2026, 19:53
08 июн 2026, 19:53
a97b6e3
Код
Авторство
О чём код?
using System.ComponentModel.DataAnnotations; using AuthApp.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; namespace AuthApp.Controllers; public class RegisterViewModel { [Required(ErrorMessage = "Full name is required.")] [Display(Name = "Full Name")] [StringLength(100)] public string FullName { get; set; } = string.Empty; [Required(ErrorMessage = "Email is required.")] [EmailAddress(ErrorMessage = "Enter a valid email address.")] [Display(Name = "Email")] public string Email { get; set; } = string.Empty; [Required(ErrorMessage = "Password is required.")] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } = string.Empty; [Required(ErrorMessage = "Please confirm your password.")] [DataType(DataType.Password)] [Display(Name = "Confirm Password")] [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] public string ConfirmPassword { get; set; } = string.Empty; } public class LoginViewModel { [Required(ErrorMessage = "Email is required.")] [EmailAddress(ErrorMessage = "Enter a valid email address.")] [Display(Name = "Email")] public string Email { get; set; } = string.Empty; [Required(ErrorMessage = "Password is required.")] [DataType(DataType.Password)] [Display(Name = "Password")] public string Password { get; set; } = string.Empty; [Display(Name = "Remember me?")] public bool RememberMe { get; set; } } 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] [AllowAnonymous] public IActionResult Register() { return View(); } // POST /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) return View(model); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FullName = model.FullName, CreatedAt = DateTime.UtcNow }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent: false); return RedirectToAction("Index", "Home"); } foreach (var error in result.Errors) ModelState.AddModelError(string.Empty, error.Description); return View(model); } // GET /Account/Login [HttpGet] [AllowAnonymous] public IActionResult Login(string? returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; return View(); } // POST /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginViewModel model, string? returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (!ModelState.IsValid) return View(model); var result = await _signInManager.PasswordSignInAsync( model.Email, model.Password, model.RememberMe, lockoutOnFailure: false); if (result.Succeeded) { if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) return Redirect(returnUrl); return RedirectToAction("Index", "Home"); } if (result.IsLockedOut) { ModelState.AddModelError(string.Empty, "This account has been locked out. Please try again later."); return View(model); } ModelState.AddModelError(string.Empty, "Invalid email or password."); return View(model); } // POST /Account/Logout [HttpPost] [Authorize] [ValidateAntiForgeryToken] public async Task<IActionResult> Logout() { await _signInManager.SignOutAsync(); return RedirectToAction("Index", "Home"); } }