public static IpInfo GetRemoteIpInfo(this HttpContext context) { #if NETSTANDARD2_0 return(IpTool.Search(context.Connection.RemoteIpAddress.ToString())); #else return(IpTool.Search(context.Request.UserHostAddress)); #endif }
/// <summary> /// get chinese ip infos /// </summary> /// <returns></returns> public static IpInfo GetCnIpInfo() { string ipAdd = GetLocalIPAddress(); if (ipAdd.StartsWith("192.168") || ipAdd.StartsWith("127")) { return(null); } return(IpTool.Search(ipAdd)); }
static void Main(string[] args) { var searcher = IpTool.Searcher; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 255; i++) { for (int j = 0; j < 255; j++) { var info = searcher.Search($"171.{i}.{j}.163"); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); var ipinfo = IpTool.Search("171.210.12.163"); Console.WriteLine(ipinfo.Country); // China Console.WriteLine(ipinfo.CountryCode); // CN Console.WriteLine(ipinfo.Province); // Sichuan Console.WriteLine(ipinfo.ProvinceCode); // SC Console.WriteLine(ipinfo.City); // Chengdu Console.WriteLine(ipinfo.Latitude); // 30.6667 Console.WriteLine(ipinfo.Longitude); // 104.6667 Console.WriteLine(ipinfo.AccuracyRadius); // 50 Console.WriteLine(); IpToolSettings.DefaultLanguage = "en"; ipinfo = IpTool.SearchWithI18N("171.210.12.163"); Console.WriteLine(ipinfo.Country); // 中国 Console.WriteLine(ipinfo.CountryCode); // CN Console.WriteLine(ipinfo.Province); // 四川省 Console.WriteLine(ipinfo.ProvinceCode); // SC Console.WriteLine(ipinfo.City); // 成都 Console.WriteLine(ipinfo.Latitude); // 30.6667 Console.WriteLine(ipinfo.Longitude); // 104.6667 Console.WriteLine(ipinfo.AccuracyRadius); // 50 Console.Read(); }
static void Main(string[] args) { IpToolSettings.LoadInternationalDbToMemory = true; Console.WriteLine("Default Searcher:" + IpTool.Search("171.210.12.163").City); Console.WriteLine("IPTools.International Searcher:" + IpTool.IpAllSearcher.Search("171.210.12.163").City); Console.WriteLine("IPTools.International Searcher with i18n:" + IpTool.IpAllSearcher.SearchWithI18N("171.210.12.163").City); Console.WriteLine("IPTools.China Searcher:" + IpTool.IpChinaSearcher.Search("171.210.12.163").City); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 255; i++) { for (int j = 0; j < 255; j++) { var info = IpTool.Search($"171.{i}.{j}.163"); } } sw.Stop(); Console.WriteLine("Query 65025 ip spend " + sw.ElapsedMilliseconds + " ms."); sw.Restart(); for (int i = 0; i < 255; i++) { for (int j = 0; j < 255; j++) { var info = IpTool.IpAllSearcher.Search($"171.{i}.{j}.163"); } } sw.Stop(); Console.WriteLine("Complex query 65025 ip spend " + sw.ElapsedMilliseconds + " ms."); Console.Read(); }
static void Main(string[] args) { var searcher = IpTool.Searcher; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 255; i++) { for (int j = 0; j < 255; j++) { var info = searcher.Search($"171.{i}.{j}.163"); } } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); var ipinfo = IpTool.Search("171.210.12.163"); Console.WriteLine(ipinfo.Country); // 中国 Console.WriteLine(ipinfo.Province); // 四川省 Console.WriteLine(ipinfo.City); // 成都市 Console.WriteLine(ipinfo.NetworkOperator); // 电信 Console.Read(); }
/// <summary> /// Get ip info from request header. /// </summary> /// <param name="context"></param> /// <param name="headerKey">request header key.</param> /// <returns></returns> public static IpInfo GetRemoteIpInfo(this HttpContext context, string headerKey) { return(IpTool.Search(context.Request.Headers[headerKey])); }
public static IpInfo GetRemoteIpInfo(this HttpContext context) { return(IpTool.Search(context.Connection.RemoteIpAddress.ToString())); }
public ActionResult Loginon(LoginDto loginModel) { if (string.IsNullOrEmpty(loginModel.uname)) { ModelState.AddModelError("err", "用户名不能为空"); } if (string.IsNullOrEmpty(loginModel.pwd)) { ModelState.AddModelError("err", "密码不能为空"); } try { login_info loginInfo = new login_info(); var userAgent = RequestHelper.UserAgent(); if (userAgent != null) { loginInfo.login_name = loginModel.uname.Trim(); loginInfo.browser = userAgent.Browser; loginInfo.device_info = userAgent.Device; loginInfo.osinfo = userAgent.OS; loginInfo.request_ip = userAgent.Ip; loginInfo.request_time = DateTime.Now; if (NetHelper.IsIntranetIP(loginInfo.request_ip)) { loginInfo.real_address = "本地局域网"; } else { var ipInfo = IpTool.Search(loginInfo.request_ip); if (ipInfo != null) { loginInfo.real_address = $"{ipInfo.Province}-{ipInfo.City}"; } //string filePath = AppDomain.CurrentDomain.BaseDirectory + @"data\ip2region.db"; //DbSearcher dbSearcher = new DbSearcher(filePath); //var dataBlock = dbSearcher.BtreeSearch("120.195.209.125"); //loginInfo.real_address = dataBlock.ToString(); } } var user = _userApp.LoginValidate(loginModel.uname.Trim(), loginModel.pwd.Trim()); var loginUserDto = new LoginUserDto(); if (user != null) { if (user.user_avatar.IsEmpty()) { user.user_avatar = "/ui/images/profile.jpg"; } loginUserDto.Id = user.id; loginUserDto.LoginName = user.login_name; loginUserDto.UserName = user.user_name; loginUserDto.IsSuper = user.is_super == 1; loginUserDto.DeptId = user.dept_id; loginUserDto.Avatar = user.user_avatar; loginUserDto.RoleId = user.role_id; loginUserDto.DeptName = user.dept_name; loginUserDto.Gender = user.gender; loginUserDto.Phone = user.mobile_phone; loginUserDto.Email = user.email; if (user.role_id > 0) { sys_role role = _roleApp.GetRoleById(user.role_id); if (role != null) { loginUserDto.RoleCode = role.role_code; loginUserDto.RoleName = role.role_name; } } //插入登录信息 loginInfo.login_status = 1; loginInfo.login_message = "登录成功"; loginInfoApp.InsertLoginInfo(loginInfo); //设置cookie // FormsAuthentication.SetAuthCookie(loginUserDto.AccountName, false); string claimstr = loginUserDto.ToJson(); RequestHelper.SetCookie(claimstr); return(Redirect("/admin/Home/Index")); } //插入登录信息 loginInfo.login_message = "用户名或密码错误"; loginInfoApp.InsertLoginInfo(loginInfo); ModelState.AddModelError("err", "用户名或密码错误"); } catch (Exception e) { LoggerHelper.Exception(e); ModelState.AddModelError("err", "登录异常"); } return(View("Index", loginModel)); }