LoginService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.Models;
  9. using TEAMModelOS.SDK.DI;
  10. using static TEAMModelOS.SDK.Models.Teacher;
  11. namespace TEAMModelOS.SDK.Models.Service
  12. {
  13. public static class LoginService
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. /// <param name="loginInfos"></param>
  19. /// <param name="expire"></param>
  20. /// <param name="now"></param>
  21. /// <param name="school"></param>
  22. /// <param name="scope">Teacher Student TmdUser</param>
  23. /// <param name="id"></param>
  24. /// <param name="ip"></param>
  25. /// <param name="region"></param>
  26. /// <returns></returns>
  27. public static async Task<List<LoginInfo>> DoLoginInfo(List<LoginInfo> loginInfos, long expire, long now, string school, string scope, string id, string ip, string region)
  28. {
  29. if (loginInfos.Any())
  30. {
  31. if (loginInfos.Count() >= 5)
  32. {
  33. loginInfos = loginInfos.OrderBy(x => x.expire).ToList();
  34. loginInfos.RemoveRange(4, loginInfos.Count() - 4);
  35. }
  36. loginInfos.Add(new LoginInfo { expire = expire, ip = ip, time = now });
  37. }
  38. else
  39. {
  40. loginInfos = new List<LoginInfo> { new LoginInfo { expire = expire, ip = ip, time = now } };
  41. }
  42. if (!string.IsNullOrWhiteSpace(school)) {
  43. //Login:School:School_ID:Teacher:20220506 value 15 1
  44. //await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:20220506", "15",1);
  45. //await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:20220506", "15", 1);
  46. }
  47. //Login:IES:20220506
  48. //var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
  49. //List<dynamic> countcds = new List<dynamic>();
  50. //if (counts != null && counts.Length > 0)
  51. //{
  52. // foreach (var count in counts)
  53. // {
  54. // countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  55. // }
  56. //}
  57. return loginInfos;
  58. }
  59. public static async Task<(string ip, string region)> LoginIp(HttpContext httpContext, IPSearcher _searcher)
  60. {
  61. var IpPort = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
  62. if (string.IsNullOrEmpty(IpPort))
  63. {
  64. IpPort = httpContext.Connection.RemoteIpAddress.ToString();
  65. }
  66. if (IpPort.Contains("::"))
  67. {
  68. IpPort = "127.0.0.1";
  69. }
  70. string ip = IpPort.Split(":")[0];
  71. string region = await _searcher.SearchIpAsync(ip);
  72. return (ip, region);
  73. }
  74. public static void LoginLog(HttpContext httpContext, Option _option, ILogger _logger, DingDing _dingDing, string ip, string region, string id, string name, int status)
  75. {
  76. httpContext.Request.Headers.TryGetValue("referer", out var referer);
  77. httpContext.Request.Headers.TryGetValue("sec-ch-ua", out var chua);
  78. httpContext.Request.Headers.TryGetValue("sec-ch-ua-platform", out var platform);
  79. httpContext.Request.Headers.TryGetValue("user-agent", out var useragent);
  80. if (status == 200)
  81. {
  82. string msg = $"{_option.Location} -- {name}({id}):Login Succeed! --Time:{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")} " +
  83. $"--Browser Info( --referer:{referer} --sec-ch-ua:{chua} --sec-ch-ua-platform:{platform} --user-agent:{useragent} --ip:{ip} --region:{region} )";
  84. _logger.LogInformation(msg);
  85. }
  86. else
  87. {
  88. string msg = $"{_option.Location} -- {name}({id}):Login Failed! --Time:{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")} " +
  89. $"--Browser Info( --referer:{referer} --sec-ch-ua:{chua} --sec-ch-ua-platform:{platform} --user-agent:{useragent} --ip:{ip} --region:{region} )";
  90. _ = _dingDing.SendBotMsg($"IES5,{_option.Location},{msg}", GroupNames.醍摩豆服務運維群組);
  91. _logger.LogError(msg);
  92. }
  93. }
  94. }
  95. }