重构代码:整理LoginUser.Ip相关代码,统一使用RemoteIpAddress ?? IPv6Loopback判断语句

This commit is contained in:
Argo Zhang 2019-03-09 19:18:03 +08:00
parent 036c6de0e3
commit 269ac3ab85
5 changed files with 7 additions and 9 deletions

View File

@ -1,7 +1,5 @@
using Bootstrap.Admin.Models;
using Bootstrap.DataAccess;
using Longbow;
using Longbow.Configuration;
using Longbow.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
@ -10,6 +8,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
@ -63,7 +62,7 @@ namespace Bootstrap.Admin.Controllers
internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, HttpContext context, LoginUser loginUser)
{
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
loginUser.Ip = context.Connection.RemoteIpAddress?.ToString();
loginUser.Ip = (context.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
loginUser.City = onlineUserSvr.RetrieveLocaleByIp(loginUser.Ip);
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}";

View File

@ -35,12 +35,11 @@ namespace Bootstrap.Admin.Controllers.Api
public bool Post([FromServices]IOnlineUsers onlineUserSvr, [FromBody]Log value)
{
var agent = new UserAgent(Request.Headers["User-Agent"]);
value.Ip = HttpContext.Connection.RemoteIpAddress?.ToString();
value.Ip = (HttpContext.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
value.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
value.OS = $"{agent.OS.Name} {agent.OS.Version}";
value.City = onlineUserSvr.RetrieveLocaleByIp(value.Ip);
value.UserName = User.Identity.Name;
if (string.IsNullOrEmpty(value.Ip)) value.Ip = IPAddress.IPv6Loopback.ToString();
return LogHelper.Save(value);
}
}

View File

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Net;
namespace Microsoft.AspNetCore.Builder
{
@ -43,7 +44,7 @@ namespace Microsoft.AspNetCore.Builder
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
var v = new OnlineUser();
v.ConnectionId = key;
v.Ip = context.Connection.RemoteIpAddress?.ToString();
v.Ip = (context.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
v.Location = onlineUserSvr.RetrieveLocaleByIp(v.Ip);
v.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
v.OS = $"{agent.OS.Name} {agent.OS.Version}";

View File

@ -39,7 +39,6 @@ namespace Bootstrap.DataAccess
Result = "登录失败"
};
config(loginUser);
if (string.IsNullOrEmpty(loginUser.Ip)) loginUser.Ip = System.Net.IPAddress.IPv6Loopback.ToString();
var ret = DbContextManager.Create<User>().Authenticate(userName, password);
if (ret) loginUser.Result = "登录成功";
LoginHelper.Log(loginUser);

View File

@ -14,7 +14,7 @@ namespace Bootstrap.DataAccess
Browser = "UnitTest",
City = "本地连接",
OS = "UnitTest",
Ip = "::",
Ip = "::1",
CRUD = "UnitTest",
RequestUrl = "~/Home/Index"
};
@ -30,7 +30,7 @@ namespace Bootstrap.DataAccess
Browser = "UnitTest",
City = "本地连接",
OS = "UnitTest",
Ip = "::",
Ip = "::1",
CRUD = "UnitTest",
RequestUrl = "~/Home/Index"
};