123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using static TEAMModelOS.SDK.Models.Teacher;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public static class LoginService
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="loginInfos"></param>
- /// <param name="expire"></param>
- /// <param name="now"></param>
- /// <param name="school"></param>
- /// <param name="scope">Teacher Student TmdUser</param>
- /// <param name="id"></param>
- /// <param name="ip"></param>
- /// <param name="region"></param>
- /// <returns></returns>
- public static async Task<List<LoginInfo>> DoLoginInfo(List<LoginInfo> loginInfos, long expire, long now, string school, string scope, string id, string ip, string region)
- {
- if (loginInfos.Any())
- {
- if (loginInfos.Count() >= 5)
- {
- loginInfos = loginInfos.OrderBy(x => x.expire).ToList();
- loginInfos.RemoveRange(4, loginInfos.Count() - 4);
- }
- loginInfos.Add(new LoginInfo { expire = expire, ip = ip, time = now });
- }
- else
- {
- loginInfos = new List<LoginInfo> { new LoginInfo { expire = expire, ip = ip, time = now } };
- }
- if (!string.IsNullOrWhiteSpace(school)) {
- //Login:School:School_ID:Teacher:20220506 value 15 1
- //await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:20220506", "15",1);
- //await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:20220506", "15", 1);
- }
- //Login:IES:20220506
- //var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
- //List<dynamic> countcds = new List<dynamic>();
- //if (counts != null && counts.Length > 0)
- //{
- // foreach (var count in counts)
- // {
- // countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
- // }
- //}
- return loginInfos;
- }
- public static async Task<(string ip, string region)> LoginIp(HttpContext httpContext, IPSearcher _searcher)
- {
- var IpPort = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
- if (string.IsNullOrEmpty(IpPort))
- {
- IpPort = httpContext.Connection.RemoteIpAddress.ToString();
- }
- if (IpPort.Contains("::"))
- {
- IpPort = "127.0.0.1";
- }
- string ip = IpPort.Split(":")[0];
- string region = await _searcher.SearchIpAsync(ip);
- return (ip, region);
- }
- public static void LoginLog(HttpContext httpContext, Option _option, ILogger _logger, DingDing _dingDing, string ip, string region, string id, string name, int status)
- {
- httpContext.Request.Headers.TryGetValue("referer", out var referer);
- httpContext.Request.Headers.TryGetValue("sec-ch-ua", out var chua);
- httpContext.Request.Headers.TryGetValue("sec-ch-ua-platform", out var platform);
- httpContext.Request.Headers.TryGetValue("user-agent", out var useragent);
- if (status == 200)
- {
- string msg = $"{_option.Location} -- {name}({id}):Login Succeed! --Time:{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")} " +
- $"--Browser Info( --referer:{referer} --sec-ch-ua:{chua} --sec-ch-ua-platform:{platform} --user-agent:{useragent} --ip:{ip} --region:{region} )";
- _logger.LogInformation(msg);
- }
- else
- {
- string msg = $"{_option.Location} -- {name}({id}):Login Failed! --Time:{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss")} " +
- $"--Browser Info( --referer:{referer} --sec-ch-ua:{chua} --sec-ch-ua-platform:{platform} --user-agent:{useragent} --ip:{ip} --region:{region} )";
- _ = _dingDing.SendBotMsg($"IES5,{_option.Location},{msg}", GroupNames.醍摩豆服務運維群組);
- _logger.LogError(msg);
- }
- }
- }
- }
|