AccountController.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using AuthorizeCenter.Services;
  2. using DllUfpEntity;
  3. using DllUfpEntity.Dto;
  4. using Microsoft.AspNetCore.Authentication;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace AuthorizeCenter.Controllers
  13. {
  14. public class AccountController : Controller
  15. {
  16. public AccountService AccountService { get; set; }
  17. public AccountController(AccountService accountService)
  18. {
  19. AccountService = accountService;
  20. }
  21. public async Task<IActionResult> Login(string returnUrl = null)
  22. {
  23. ViewData["ReturnUrl"] = returnUrl;
  24. return View();
  25. }
  26. [HttpPost]
  27. public async Task<IActionResult> Login(UserLoginDto userLoginDto)
  28. {
  29. ViewData["ReturnUrl"] = userLoginDto.ReturnUrl;
  30. string errorinfo = string.Empty;
  31. var res = AccountService.Login(new Staff()
  32. {
  33. FCode = userLoginDto.Account,
  34. Password = userLoginDto.Password
  35. }, ref errorinfo);
  36. if (res != null)
  37. {
  38. AuthenticationProperties props = new AuthenticationProperties
  39. {
  40. IsPersistent = true,
  41. ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromDays(1))
  42. };
  43. await HttpContext.SignInAsync(res.ID.ToString(), new System.Security.Claims.ClaimsPrincipal(new BinaryReader(new MemoryStream(Encoding.UTF8.GetBytes(res.FCode)))), props);
  44. if (!string.IsNullOrEmpty(userLoginDto.ReturnUrl))
  45. {
  46. return Redirect(userLoginDto.ReturnUrl);
  47. }
  48. return View();
  49. }
  50. else
  51. {
  52. return View();
  53. }
  54. }
  55. }
  56. }