修改解析IP区域位置的库 以及异地登录判断
parent
29d0ebdb7c
commit
1fef5509d1
|
@ -0,0 +1,42 @@
|
||||||
|
using IRaCIS.Core.Domain.Models;
|
||||||
|
using MaxMind.GeoIP2;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IRaCIS.Core.Application.Helper
|
||||||
|
{
|
||||||
|
public static class IPCityHelper
|
||||||
|
{
|
||||||
|
|
||||||
|
public static string GetCityResponse(string ip)
|
||||||
|
{
|
||||||
|
var path = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, "GeoLite2-City.mmdb");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var reader = new DatabaseReader(path))
|
||||||
|
{
|
||||||
|
|
||||||
|
var city = reader.City(ip);
|
||||||
|
|
||||||
|
//Console.WriteLine(city.Country.IsoCode); // 'US' 'CN'
|
||||||
|
//Console.WriteLine(city.Country.Name); // 'United States' 'China'
|
||||||
|
////Console.WriteLine(city.Country.Names["zh-CN"]); // '美国'
|
||||||
|
//Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei'
|
||||||
|
//Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan'
|
||||||
|
|
||||||
|
return $"{city.Country.Name} | {city.MostSpecificSubdivision.Name} | {city.City.Name}";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
|
||||||
|
return $"UN | UN | {ip}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -48,7 +48,7 @@ namespace IRaCIS.Core.Application.ViewModel
|
||||||
{
|
{
|
||||||
public Guid? TrialId { get; set; }
|
public Guid? TrialId { get; set; }
|
||||||
|
|
||||||
public List<UserOptType> OptTypeList { get; set; }
|
public List<UserOptType>? OptTypeList { get; set; }
|
||||||
|
|
||||||
public string? IP { get; set; }
|
public string? IP { get; set; }
|
||||||
|
|
||||||
|
|
|
@ -850,7 +850,7 @@ namespace IRaCIS.Core.Application.Service
|
||||||
.WhereIf(inQuery.TargetIdentityUserId != null, t => t.TargetIdentityUserId == inQuery.TargetIdentityUserId)
|
.WhereIf(inQuery.TargetIdentityUserId != null, t => t.TargetIdentityUserId == inQuery.TargetIdentityUserId)
|
||||||
.WhereIf(inQuery.TrialId != null, t => t.ActionIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId) || t.TargetIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId))
|
.WhereIf(inQuery.TrialId != null, t => t.ActionIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId) || t.TargetIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId))
|
||||||
.WhereIf(trialCreateTime != null, t => t.CreateTime >= trialCreateTime)
|
.WhereIf(trialCreateTime != null, t => t.CreateTime >= trialCreateTime)
|
||||||
.WhereIf(inQuery.OptTypeList != null && inQuery.OptTypeList.Count>0, t => inQuery.OptTypeList.Contains(t.OptType))
|
.WhereIf(inQuery.OptTypeList != null && inQuery.OptTypeList.Count > 0, t => inQuery.OptTypeList.Contains(t.OptType))
|
||||||
.WhereIf(inQuery.BeginDate != null, t => t.CreateTime >= inQuery.BeginDate)
|
.WhereIf(inQuery.BeginDate != null, t => t.CreateTime >= inQuery.BeginDate)
|
||||||
.WhereIf(inQuery.EndDate != null, t => t.CreateTime <= inQuery.EndDate)
|
.WhereIf(inQuery.EndDate != null, t => t.CreateTime <= inQuery.EndDate)
|
||||||
.WhereIf(inQuery.IsLoginUncommonly != null, t => t.IsLoginUncommonly == inQuery.IsLoginUncommonly)
|
.WhereIf(inQuery.IsLoginUncommonly != null, t => t.IsLoginUncommonly == inQuery.IsLoginUncommonly)
|
||||||
|
@ -958,29 +958,31 @@ namespace IRaCIS.Core.Application.Service
|
||||||
//账号在系统存在
|
//账号在系统存在
|
||||||
if (isExistAccount || loginUser != null)
|
if (isExistAccount || loginUser != null)
|
||||||
{
|
{
|
||||||
var ipinfo = _searcher.Search(_userInfo.IP);
|
//var ipinfo = _searcher.Search(_userInfo.IP);
|
||||||
|
|
||||||
var iPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
|
//var iPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
|
||||||
|
|
||||||
string SplitAndConcatenate(string input)
|
//string SplitAndConcatenate(string input)
|
||||||
|
//{
|
||||||
|
// string[] parts = input.Split('|');
|
||||||
|
// return parts.Length >= 3 ? parts[0] + parts[1] : string.Join("", parts);
|
||||||
|
//}
|
||||||
|
|
||||||
|
var iPRegion = IPCityHelper.GetCityResponse(_userInfo.IP);
|
||||||
|
|
||||||
|
//设置本次登录的IP
|
||||||
|
await _identityUserRepository.BatchUpdateNoTrackingAsync(x => x.Id == existUserLoginInfo.Id, x => new IdentityUser()
|
||||||
{
|
{
|
||||||
string[] parts = input.Split('|');
|
LastLoginIP = iPRegion,
|
||||||
return parts.Length >= 3 ? parts[0] + parts[1] : string.Join("", parts);
|
LastLoginTime = DateTime.Now
|
||||||
}
|
|
||||||
|
});
|
||||||
|
|
||||||
if (existUserLoginInfo.LastLoginIP != string.Empty)
|
if (existUserLoginInfo.LastLoginIP != string.Empty)
|
||||||
{
|
{
|
||||||
//设置上次登录的IP
|
|
||||||
await _identityUserRepository.BatchUpdateNoTrackingAsync(x => x.Id == existUserLoginInfo.Id, x => new IdentityUser()
|
|
||||||
{
|
|
||||||
LastLoginIP = iPRegion,
|
|
||||||
LastLoginTime = DateTime.Now
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// 与上一次区域不一致
|
// 与上一次区域不一致
|
||||||
if (SplitAndConcatenate(existUserLoginInfo.LastLoginIP) != SplitAndConcatenate(iPRegion))
|
//if (SplitAndConcatenate(existUserLoginInfo.LastLoginIP) != SplitAndConcatenate(iPRegion))
|
||||||
|
if (existUserLoginInfo.LastLoginIP != iPRegion)
|
||||||
{
|
{
|
||||||
|
|
||||||
isLoginUncommonly = true;
|
isLoginUncommonly = true;
|
||||||
|
|
|
@ -106,34 +106,12 @@ namespace IRaCIS.Core.Application.Service
|
||||||
|
|
||||||
foreach (var ip in ipList)
|
foreach (var ip in ipList)
|
||||||
{
|
{
|
||||||
try
|
var ipRegion = IPCityHelper.GetCityResponse(ip);
|
||||||
{
|
|
||||||
using (var reader = new DatabaseReader(path))
|
|
||||||
{
|
|
||||||
|
|
||||||
var city = reader.City(ip);
|
await _userLogRepository.BatchUpdateNoTrackingAsync(t => t.IP == ip, u => new UserLog() { IPRegion = ipRegion });
|
||||||
|
|
||||||
Console.WriteLine(city.Country.IsoCode); // 'US' 'CN'
|
|
||||||
Console.WriteLine(city.Country.Name); // 'United States' 'China'
|
|
||||||
//Console.WriteLine(city.Country.Names["zh-CN"]); // '美国'
|
|
||||||
Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei'
|
|
||||||
Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan'
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine("----------------");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
|
|
||||||
Console.WriteLine($"{ip}在数据库无法解析 {ex.Message}"); // 44.9733
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return ResponseOutput.Ok();
|
return ResponseOutput.Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using EntityFrameworkCore.Triggered;
|
using EntityFrameworkCore.Triggered;
|
||||||
using IP2Region.Net.Abstractions;
|
using IP2Region.Net.Abstractions;
|
||||||
|
using IRaCIS.Core.Application.Helper;
|
||||||
|
using MaxMind.GeoIP2;
|
||||||
|
|
||||||
namespace IRaCIS.Core.Application.Triggers
|
namespace IRaCIS.Core.Application.Triggers
|
||||||
{
|
{
|
||||||
|
@ -18,10 +20,11 @@ namespace IRaCIS.Core.Application.Triggers
|
||||||
if (context.ChangeType == ChangeType.Added)
|
if (context.ChangeType == ChangeType.Added)
|
||||||
{
|
{
|
||||||
|
|
||||||
var ipinfo = _searcher.Search(userLog.IP);
|
//var ipinfo = _searcher.Search(userLog.IP);
|
||||||
|
|
||||||
userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
|
//userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3));
|
||||||
|
|
||||||
|
userLog.IPRegion = IPCityHelper.GetCityResponse(userLog.IP);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue