AccountController adding login register [closed]

6 days ago 4
ARTICLE AD BOX
[HttpGet] public IActionResult Register() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Register(RegisterVm register) { if (!ModelState.IsValid) return View(register); AppUser appUser = new AppUser() { Email = register.Email, UserName = register.UserName, Name = register.Name, Surname=register.Surname }; IdentityResult identityResult = await _userManager.CreateAsync(appUser, register.Password); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { ModelState.AddModelError("", error.Description); } return View(register); } //await _userManager.AddToRoleAsync(appUser, nameof(Roles.Member)); await _signInManager.SignInAsync(appUser, true); return RedirectToAction("Index", "Home"); } public async Task<IActionResult> Logout() { await _signInManager.SignOutAsync(); return RedirectToAction("Index","Home"); } [HttpGet] public IActionResult Login() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(LoginVm login) { if (!ModelState.IsValid) return View(); AppUser user = await _userManager.FindByEmailAsync(login.UserNameOrEmail) ?? await _userManager.FindByNameAsync(login.UserNameOrEmail); if (user is null) { ModelState.AddModelError("", "Email or password incorrect"); return View(); } var signInResult = await _signInManager.PasswordSignInAsync(user, login.Password, false, false); if (!signInResult.Succeeded) { ModelState.AddModelError("", "Email or password incorrect"); return View(); } await _signInManager.SignInAsync(user, false); return RedirectToAction("Index","Home"); }=============================================================== [Area("Admin")] public class DashboardController : Controller { public IActionResult Index() { return View(); }=================================================================== [Area("Admin")] public class PositionController : Controller { private readonly AppDbContext _context; public PositionController(AppDbContext context) { _context = context; } public async Task<IActionResult> Index() { PositionVm positionVm = new() { Positions = await _context.Positions.Where(p=>p.IsDeleted==false).ToListAsync() }; return View(positionVm); } public IActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create(CreatePositionVm createPositionVm) { if (createPositionVm == null) return NotFound(); if (!ModelState.IsValid) { return View(createPositionVm); } Position newPosition = new() {Name=createPositionVm.Name, IsDeleted=false }; await _context.Positions.AddAsync(newPosition); await _context.SaveChangesAsync(); return RedirectToAction("Index", "Position"); } public async Task<IActionResult> Detail(int? id) { if (id == null) return NotFound(); var position = await _context.Positions.FindAsync(id); if (position == null) return NotFound(); return View(position); } public async Task<IActionResult> Edit(int? id) { if (id == null) return NotFound(); Position position = await _context.Positions.FindAsync(id); if (position == null) return NotFound(); return View(position); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(Position updatePosition,int? id) { if (id == null) return NotFound(); Position dbposition = await _context.Positions.FindAsync(id); if (dbposition == null) return NotFound(); if (ModelState["Name"].ValidationState==ModelValidationState.Invalid) { return NotFound(); } dbposition.Name = updatePosition.Name; await _context.SaveChangesAsync(); return RedirectToAction("Index", "Position"); } public async Task<IActionResult> Delete(int? id) { if(id==null)return NotFound(); Position position= _context.Positions.Find(id); if (position == null) return NotFound(); position.IsDeleted = true; await _context.SaveChangesAsync(); return RedirectToAction("Index","Position"); }================================================ [Area("Admin")] public class TeamController : Controller { private readonly AppDbContext _context; private IWebHostEnvironment _webHostEnvironment; public TeamController(AppDbContext context, IWebHostEnvironment webHostEnvironment) { _context = context; _webHostEnvironment = webHostEnvironment; } public async Task<IActionResult> Index() { TeamVm teamVm = new() { Teams = await _context.Team.Where(t => t.IsDeleted == false).Include(t => t.Position).ToListAsync(), Positions = await _context.Positions.Where(p=>p.IsDeleted==false).ToListAsync() }; return View(teamVm); } public IActionResult Create() { ViewBag.Positions = _context.Positions.Where(p => p.IsDeleted == false); return View(); } [HttpPost] public async Task<IActionResult> Create(CreateTeamVm team,int positionId) { if (!ModelState.IsValid) { ViewBag.Positions = _context.Positions.Where(p=>p.IsDeleted==false).ToList(); return View(team); } if (team.Photo == null || team.Photo.Length == 0) { ModelState.AddModelError("Photo", "Şəkil seçilməyib"); ViewBag.Positions = _context.Positions.ToList(); return View(team); } if (!team.Photo.ContentType.StartsWith("image/")) { ModelState.AddModelError("Photo", "Yalnız şəkil fayllarına icazə verilir"); return View(team); } if (team.Photo.Length / 1024.0 / 1024.0 > 2) { ModelState.AddModelError("Photo", "Şəklin ölçüsü maksimum 2 MB olmalıdır"); return View(team); } // 🔹 FILE SAVE string extension = Path.GetExtension(team.Photo.FileName); string fileName = $"{Guid.NewGuid()}{extension}"; string folder = "uploads"; string path = Path.Combine(_webHostEnvironment.WebRootPath, folder); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fullPath = Path.Combine(path, fileName); using (FileStream stream = new FileStream(fullPath, FileMode.Create)) { await team.Photo.CopyToAsync(stream); } // 🔹 ENTITY YARAT Team newTeam = new Team { Name = team.Name, Surname = team.Surname, PositionId = team.PositionId, Image = $"{folder}/{fileName}" }; await _context.Team.AddAsync(newTeam); await _context.SaveChangesAsync(); return RedirectToAction("Index"); }=
Read Entire Article