重构代码:整理LoginUser.Ip相关代码,统一使用RemoteIpAddress ?? IPv6Loopback判断语句
This commit is contained in:
parent
036c6de0e3
commit
269ac3ab85
|
@ -1,7 +1,5 @@
|
||||||
using Bootstrap.Admin.Models;
|
using Bootstrap.Admin.Models;
|
||||||
using Bootstrap.DataAccess;
|
using Bootstrap.DataAccess;
|
||||||
using Longbow;
|
|
||||||
using Longbow.Configuration;
|
|
||||||
using Longbow.Web;
|
using Longbow.Web;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
|
@ -10,6 +8,7 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -63,7 +62,7 @@ namespace Bootstrap.Admin.Controllers
|
||||||
internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, HttpContext context, LoginUser loginUser)
|
internal static void CreateLoginUser(IOnlineUsers onlineUserSvr, HttpContext context, LoginUser loginUser)
|
||||||
{
|
{
|
||||||
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
|
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.City = onlineUserSvr.RetrieveLocaleByIp(loginUser.Ip);
|
||||||
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
|
loginUser.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
|
||||||
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
loginUser.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
||||||
|
|
|
@ -35,12 +35,11 @@ namespace Bootstrap.Admin.Controllers.Api
|
||||||
public bool Post([FromServices]IOnlineUsers onlineUserSvr, [FromBody]Log value)
|
public bool Post([FromServices]IOnlineUsers onlineUserSvr, [FromBody]Log value)
|
||||||
{
|
{
|
||||||
var agent = new UserAgent(Request.Headers["User-Agent"]);
|
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.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
|
||||||
value.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
value.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
||||||
value.City = onlineUserSvr.RetrieveLocaleByIp(value.Ip);
|
value.City = onlineUserSvr.RetrieveLocaleByIp(value.Ip);
|
||||||
value.UserName = User.Identity.Name;
|
value.UserName = User.Identity.Name;
|
||||||
if (string.IsNullOrEmpty(value.Ip)) value.Ip = IPAddress.IPv6Loopback.ToString();
|
|
||||||
return LogHelper.Save(value);
|
return LogHelper.Save(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
{
|
{
|
||||||
|
@ -43,7 +44,7 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
|
var agent = new UserAgent(context.Request.Headers["User-Agent"]);
|
||||||
var v = new OnlineUser();
|
var v = new OnlineUser();
|
||||||
v.ConnectionId = key;
|
v.ConnectionId = key;
|
||||||
v.Ip = context.Connection.RemoteIpAddress?.ToString();
|
v.Ip = (context.Connection.RemoteIpAddress ?? IPAddress.IPv6Loopback).ToString();
|
||||||
v.Location = onlineUserSvr.RetrieveLocaleByIp(v.Ip);
|
v.Location = onlineUserSvr.RetrieveLocaleByIp(v.Ip);
|
||||||
v.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
|
v.Browser = $"{agent.Browser.Name} {agent.Browser.Version}";
|
||||||
v.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
v.OS = $"{agent.OS.Name} {agent.OS.Version}";
|
||||||
|
|
|
@ -39,7 +39,6 @@ namespace Bootstrap.DataAccess
|
||||||
Result = "登录失败"
|
Result = "登录失败"
|
||||||
};
|
};
|
||||||
config(loginUser);
|
config(loginUser);
|
||||||
if (string.IsNullOrEmpty(loginUser.Ip)) loginUser.Ip = System.Net.IPAddress.IPv6Loopback.ToString();
|
|
||||||
var ret = DbContextManager.Create<User>().Authenticate(userName, password);
|
var ret = DbContextManager.Create<User>().Authenticate(userName, password);
|
||||||
if (ret) loginUser.Result = "登录成功";
|
if (ret) loginUser.Result = "登录成功";
|
||||||
LoginHelper.Log(loginUser);
|
LoginHelper.Log(loginUser);
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Bootstrap.DataAccess
|
||||||
Browser = "UnitTest",
|
Browser = "UnitTest",
|
||||||
City = "本地连接",
|
City = "本地连接",
|
||||||
OS = "UnitTest",
|
OS = "UnitTest",
|
||||||
Ip = "::",
|
Ip = "::1",
|
||||||
CRUD = "UnitTest",
|
CRUD = "UnitTest",
|
||||||
RequestUrl = "~/Home/Index"
|
RequestUrl = "~/Home/Index"
|
||||||
};
|
};
|
||||||
|
@ -30,7 +30,7 @@ namespace Bootstrap.DataAccess
|
||||||
Browser = "UnitTest",
|
Browser = "UnitTest",
|
||||||
City = "本地连接",
|
City = "本地连接",
|
||||||
OS = "UnitTest",
|
OS = "UnitTest",
|
||||||
Ip = "::",
|
Ip = "::1",
|
||||||
CRUD = "UnitTest",
|
CRUD = "UnitTest",
|
||||||
RequestUrl = "~/Home/Index"
|
RequestUrl = "~/Home/Index"
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue