using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using APP.Models; using APP.Utils; using BWPMModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; namespace APP.Controllers { public class AccountController : Controller { private readonly UserManager _userManager; private readonly SignInManager _signInManager; public AccountController(UserManager userManager, SignInManager signInManager) { this._userManager = userManager; this._signInManager = signInManager; } [HttpGet] public IActionResult Login() { return View(); } [HttpGet] //[Authorize] public IActionResult Welcome() { ViewBag.sessionvariable = HttpContext.Session.GetString("SessionVariable1"); return View(); } [HttpPost] public async Task Login(LoginViewModel model) { if (!ModelState.IsValid) { return View(model); } model.Email = model.Email.Trim(); var appUser = await _userManager.FindByEmailAsync(model.Email).ConfigureAwait(false); if (appUser == null) { ModelState.AddModelError("FileNameValidation", "Account does not exist."); return View(model); } var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, true, lockoutOnFailure: false).ConfigureAwait(false); if (result.Succeeded) { HttpContext.Session.SetString("LoggendIn", "True"); return RedirectToAction("Welcome", "Account"); } ModelState.AddModelError("FileNameValidation", "Password does not match."); ViewBag.message = "Ungültige Eingabe von User/Passwort"; return View(model); } public async Task Logout() { await _signInManager.SignOutAsync().ConfigureAwait(false); HttpContext.Session.SetString("LoggendIn", "false"); return Redirect("/"); } } }