using Bootstrap.Admin.Models; using Bootstrap.Security; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace Bootstrap.Admin.Controllers { /// /// Account controller. /// [AllowAnonymous] [AutoValidateAntiforgeryToken] public class AccountController : Controller { /// /// /// /// [HttpGet] public ActionResult Login() { return View("Login", new ModelBase()); } /// /// Login the specified userName, password and remember. /// /// The login. /// User name. /// Password. /// Remember. [HttpPost] public async Task Login(string userName, string password, string remember) { if (BootstrapUser.Authenticate(userName, password)) { var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(ClaimTypes.Name, userName)); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties() { IsPersistent = remember == "true" }); } // redirect origin url var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].FirstOrDefault() ?? "~/Home/Index"; return Redirect(originUrl); } /// /// Logout this instance. /// /// The logout. public async Task Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return Redirect("~" + CookieAuthenticationDefaults.LoginPath); } /// /// Accesses the denied. /// /// The denied. [ResponseCache(Duration = 600)] public ActionResult AccessDenied() { return View(); } /// /// /// /// public ActionResult Mobile() { return View(); } } }