PublicMethodController.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using System.DrawingCore.Imaging;
  10. using System.IO;
  11. using Library;
  12. // For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  13. namespace MySystem.Areas.Api.Controllers
  14. {
  15. [Area("Api")]
  16. [Route("Api/[controller]/[action]")]
  17. public class PublicMethodController : BaseController
  18. {
  19. public PublicMethodController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  20. {
  21. }
  22. #region 图片验证码
  23. public FileContentResult CheckCode()
  24. {
  25. string code = function.get_Random(4);
  26. var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 120, 40);
  27. MemoryStream stream = new MemoryStream();
  28. bitmap.Save(stream, ImageFormat.Gif);
  29. function.WriteCookie(_accessor.HttpContext, "checkcode", code);
  30. return File(stream.ToArray(), "image/gif");
  31. }
  32. #endregion
  33. #region 图片验证码
  34. public FileContentResult PictureCode(string Tag = "")
  35. {
  36. string code = function.get_Random(4);
  37. var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 120, 40);
  38. MemoryStream stream = new MemoryStream();
  39. bitmap.Save(stream, ImageFormat.Gif);
  40. RedisDbconn.Instance.Set(Tag, code);
  41. RedisDbconn.Instance.SetExpire(Tag, 600);
  42. return File(stream.ToArray(), "image/jpg");
  43. }
  44. #endregion
  45. }
  46. }