HomeController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Diagnostics;
  3. using System.ServiceModel;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using ReportWeb;
  8. using ReportWeb.Models;
  9. using SuperAdmin.Models;
  10. using SuperAdmin.Models.Http;
  11. using SuperAdmin.Models.Vsa;
  12. using SuperAdmin.Source;
  13. using SuperCms.Extensions;
  14. using SvVsa;
  15. namespace SuperAdmin.Controllers
  16. {
  17. public class HomeController : BaseController
  18. {
  19. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  20. DbConnector db = new DbConnector();
  21. public static int counter = 0;
  22. public static bool counterStart = false;
  23. public static string serviceName;
  24. //public static string subDomain;
  25. public HomeController(IConfiguration _configuration, IWebHostEnvironment hostEnvironment) : base(_configuration, hostEnvironment)
  26. {
  27. // init
  28. }
  29. public IActionResult Index()
  30. {
  31. serviceName = GetParameter(UtilsController.Constant.SERVICE_NAME);
  32. //subDomain = GetParameter(UtilsController.Constant.SUB_DOMAIN);
  33. HttpContext.Session.SetComplexData("serviceName", serviceName);
  34. HttpContext.Session.SetComplexData("subDomain", BaseController.subDomain);
  35. if (!CheckAuthToken())
  36. {
  37. return Redirect(GetParameter(UtilsController.Constant.SUB_DOMAIN) + "/Home/Login");
  38. }
  39. return View();
  40. }
  41. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  42. public IActionResult Error()
  43. {
  44. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  45. }
  46. public IActionResult Login()
  47. {
  48. return View();
  49. }
  50. public IActionResult LoginAction(String account, String password)
  51. {
  52. var rs = db.GetUserByName(account);
  53. if (rs == null || rs.Count == 0)
  54. {
  55. log.Info("Not found user " + account);
  56. ViewBag.username = "Welcome!";
  57. ViewBag.content = "Authen failed";
  58. return View("Login");
  59. }
  60. Users user = rs[0];
  61. // check password
  62. if (user.password != Common.EncryptPassword(account, password))
  63. {
  64. log.Info("Authen failed " + account);
  65. ViewBag.username = "Welcome!";
  66. ViewBag.content = "Authen failed";
  67. return View("Login");
  68. }
  69. // success
  70. CreateAuthToken(account, user);
  71. ViewBag.errorMessageCss = "none";
  72. string displayName = account;
  73. ViewBag.username = "Welcome, " + displayName;
  74. return Redirect(subDomain + "/Admin/Index");
  75. }
  76. public IActionResult Logout()
  77. {
  78. try
  79. {
  80. ClearCache();
  81. return View("Login");
  82. }
  83. catch (Exception ex)
  84. {
  85. log.Error("Exception " + ex);
  86. return Redirect(subDomain + "/Shared/Error");
  87. }
  88. }
  89. }
  90. }