| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using GreenTree.Strohrmann.ERP.Core.Helper;
- using GreenTree.Strohrmann.ERP.Domain.Model;
- using GreenTree.Strohrmann.ERP.Services.Authentication;
- using GreenTree.Strohrmann.ERP.Services.Authorization;
- using GreenTree.Strohrmann.ERP.Web.Models.Account;
- using Microsoft.AspNetCore.Authentication.Cookies;
- using Microsoft.AspNetCore.Mvc;
- namespace GreenTree.Strohrmann.ERP.Web.Controllers
- {
- public class AccountController : Controller
- {
- #region DI fields
- // The global DbContext
- private readonly ERPDbContext _eRPDbContext;
- // The global authentication service
- private readonly IAuthenticationService _authenticationService;
- // The global user helper
- private readonly IUserHelper _userHelper;
- // The global administation options
- private readonly AdministrationOptions _administrationOptions;
- #endregion
- #region Ctor
- /// <summary>
- /// Initializes a new instance of the AccountController class
- /// </summary>
- /// <param name="eRPDbContext">Global DbContext.</param>
- /// <param name="authenticationService">Global authentication service.</param>
- /// <param name="userHelper">Global user helper.</param>
- /// <param name="administrationOptions">Global administration options.</param>
- public AccountController(
- ERPDbContext eRPDbContext,
- IAuthenticationService authenticationService,
- IUserHelper userHelper,
- AdministrationOptions administrationOptions)
- {
- _eRPDbContext = eRPDbContext;
- _authenticationService = authenticationService;
- _userHelper = userHelper;
- _administrationOptions = administrationOptions;
- }
- #endregion
- #region Actions
- // Index View / redirect to Login
- public IActionResult Index()
- {
- return RedirectToAction(nameof(Login));
- }
- // Login View
- public IActionResult Login()
- {
- return View("~/Views/Account/Login.cshtml");
- }
- // POST: Account/Login
- [HttpPost]
- [ValidateAntiForgeryToken]
- public IActionResult Login(LoginModel login)
- {
- if (!ModelState.IsValid)
- {
- login.Password = String.Empty;
- return View("~/Views/Account/Login.cshtml", login);
- }
- if (login.Username == _administrationOptions.Administrator)
- {
- _authenticationService.SignInAdmin(login.StayLoggedIn);
- return RedirectToAction("Index", "Home");
- }
- else
- {
- _authenticationService.SignIn(login.Username, login.StayLoggedIn);
- return RedirectToAction("Index", "Home");
- }
- }
- // Logout View
- public IActionResult Logout()
- {
- _authenticationService.SignOut();
- return RedirectToAction(nameof(Login));
- }
- #endregion
- }
- }
|