/* * OCPP.Core - https://github.com/dallmann-consulting/OCPP.Core * Copyright (C) 2020-2021 dallmann consulting GmbH. * All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using OCPP.Core.Database; using OCPP.Core.Management.Models; namespace OCPP.Core.Management.Controllers { [Authorize] public class AccountController : BaseController { public AccountController( UserManager userManager, ILoggerFactory loggerFactory, IConfiguration config, OCPPCoreContext dbContext) : base(userManager, loggerFactory, config, dbContext) { Logger = loggerFactory.CreateLogger(); } // 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 Login(UserModel userModel, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true await UserManager.SignIn(this.HttpContext, userModel, false); if (userModel != null && !string.IsNullOrWhiteSpace(userModel.Username)) { Logger.LogInformation("User '{0}' logged in", userModel.Username); return RedirectToLocal(returnUrl); } else { Logger.LogInformation("Invalid login attempt: User '{0}'", userModel.Username); ModelState.AddModelError(string.Empty, "Invalid login attempt"); return View(userModel); } } // If we got this far, something failed, redisplay form return View(userModel); } [AllowAnonymous] public async Task Logout(UserModel userModel) { Logger.LogInformation("Signing our user '{0}'", userModel.Username); await UserManager.SignOut(this.HttpContext); return RedirectToAction(nameof(AccountController.Login), "Account"); } private IActionResult RedirectToLocal(string returnUrl) { if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction(nameof(HomeController.Index), Constants.HomeController); } } } }