AccountController.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Auth.Models;
  2. using Long.Common.Basedata;
  3. using Long.DAO.Basedata;
  4. using Long.Utils.Encryptor;
  5. using Microsoft.AspNetCore.Authentication;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using System.Runtime.CompilerServices;
  9. namespace Auth.Controllers;
  10. public class AccountController : Controller
  11. {
  12. private readonly ISystemUserDatabase _systemUserDatabase = new SystemUserDatabase();
  13. private readonly IUserDatabase _userDatabase = new UserDatabase();
  14. private string _encryptionKey;
  15. public AccountController(IConfiguration configuration)
  16. {
  17. this._encryptionKey = configuration["EncryptorKey"] ?? "";
  18. }
  19. /// <summary>
  20. /// 登录页
  21. /// </summary>
  22. /// <param name="returnUrl"></param>
  23. /// <returns></returns>
  24. [HttpGet]
  25. public IActionResult Login(string returnUrl)
  26. {
  27. return View(returnUrl);
  28. }
  29. /// <summary>
  30. /// 登录
  31. /// </summary>
  32. /// <param name="loginModel"></param>
  33. /// <returns></returns>
  34. /// <exception cref="ApplicationException"></exception>
  35. [HttpPost]
  36. public async Task<IActionResult> Login(LoginModel loginModel)
  37. {
  38. if (!ModelState.IsValid) {
  39. throw new ApplicationException("数据不完整");
  40. }
  41. if (loginModel.ClientId == "admin")
  42. {
  43. // 系统用户登录
  44. var systemUser = await _systemUserDatabase.GetByUserNameAsync(loginModel.UserName);
  45. if (systemUser != null && DESEncryptor.Encrypt(loginModel.Password, _encryptionKey) == systemUser.Password)
  46. {
  47. AuthenticationProperties props = new AuthenticationProperties
  48. {
  49. IsPersistent = true,
  50. ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromDays(1))
  51. };
  52. //await HttpContext.SignInAsync(systemUser.Id.ToString(),, props);
  53. }
  54. }
  55. return Json(null);
  56. }
  57. }