AccountController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Threading.Tasks;
  6. using GreenTree.Maschinenbestellungen.Core.Helper;
  7. using GreenTree.Maschinenbestellungen.Domain.Model;
  8. using GreenTree.Maschinenbestellungen.Services.Authentication;
  9. using GreenTree.Maschinenbestellungen.Services.Authorization;
  10. using GreenTree.Maschinenbestellungen.Web.Models.Account;
  11. using Microsoft.AspNetCore.Authentication.Cookies;
  12. using Microsoft.AspNetCore.Mvc;
  13. namespace GreenTree.Maschinenbestellungen.Web.Controllers
  14. {
  15. public class AccountController : Controller
  16. {
  17. #region DI fields
  18. // The global DbContext
  19. private readonly OrderDbContext _eRPDbContext;
  20. // The global authentication service
  21. private readonly IAuthenticationService _authenticationService;
  22. // The global user helper
  23. private readonly IUserHelper _userHelper;
  24. // The global administation options
  25. private readonly AdministrationOptions _administrationOptions;
  26. #endregion
  27. #region Ctor
  28. /// <summary>
  29. /// Initializes a new instance of the AccountController class
  30. /// </summary>
  31. /// <param name="eRPDbContext">Global DbContext.</param>
  32. /// <param name="authenticationService">Global authentication service.</param>
  33. /// <param name="userHelper">Global user helper.</param>
  34. /// <param name="administrationOptions">Global administration options.</param>
  35. public AccountController(
  36. OrderDbContext eRPDbContext,
  37. IAuthenticationService authenticationService,
  38. IUserHelper userHelper,
  39. AdministrationOptions administrationOptions)
  40. {
  41. _eRPDbContext = eRPDbContext;
  42. _authenticationService = authenticationService;
  43. _userHelper = userHelper;
  44. _administrationOptions = administrationOptions;
  45. }
  46. #endregion
  47. #region Actions
  48. // Index View / redirect to Login
  49. public IActionResult Index()
  50. {
  51. return RedirectToAction(nameof(Login));
  52. }
  53. // Login View
  54. public IActionResult Login()
  55. {
  56. return View("~/Views/Account/Login.cshtml");
  57. }
  58. // POST: Account/Login
  59. [HttpPost]
  60. [ValidateAntiForgeryToken]
  61. public IActionResult Login(LoginModel login)
  62. {
  63. if (!ModelState.IsValid)
  64. {
  65. login.Password = String.Empty;
  66. return View("~/Views/Account/Login.cshtml", login);
  67. }
  68. if (login.Username == _administrationOptions.Administrator)
  69. {
  70. _authenticationService.SignInAdmin(login.StayLoggedIn);
  71. return RedirectToAction("Index", "Home");
  72. }
  73. else
  74. {
  75. _authenticationService.SignIn(login.Username, login.StayLoggedIn);
  76. return RedirectToAction("Index", "Home");
  77. }
  78. }
  79. // Logout View
  80. public IActionResult Logout()
  81. {
  82. _authenticationService.SignOut();
  83. return RedirectToAction(nameof(Login));
  84. }
  85. #endregion
  86. }
  87. }