From 036c6de0e3683fa35670f9339394b73f0c83e312 Mon Sep 17 00:00:00 2001 From: Argo Zhang <5196060@qq.com> Date: Sat, 9 Mar 2019 19:28:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=9A=E6=9B=B4=E6=96=B0AccountTest=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=20link=20#ISHLV?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Commit commit:99b8713 --- UnitTest/Bootstrap.Admin/Controllers/AccountTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitTest/Bootstrap.Admin/Controllers/AccountTest.cs b/UnitTest/Bootstrap.Admin/Controllers/AccountTest.cs index 0acf2fa8..cb423871 100644 --- a/UnitTest/Bootstrap.Admin/Controllers/AccountTest.cs +++ b/UnitTest/Bootstrap.Admin/Controllers/AccountTest.cs @@ -41,7 +41,7 @@ namespace Bootstrap.Admin.Controllers var r = await Client.GetAsync("AccessDenied"); Assert.True(r.IsSuccessStatusCode); var content = await r.Content.ReadAsStringAsync(); - Assert.Contains("您无权访问本页面请联系网站管理员授权后再查看", content); + Assert.Contains("服务器拒绝处理您的请求", content); } } } From 269ac3ab85b1064c8ca501e1118ce5d8ccb44e1b Mon Sep 17 00:00:00 2001 From: Argo Zhang <5196060@qq.com> Date: Sat, 9 Mar 2019 19:18:03 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=9A=E6=95=B4=E7=90=86LoginUser.Ip=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8?= =?UTF-8?q?RemoteIpAddress=20=3F=3F=20IPv6Loopback=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Controllers/AccountController.cs | 5 ++--- Bootstrap.Admin/Controllers/Api/LogsController.cs | 3 +-- .../OnlineUsers/OnlineUsersMiddlewareExtensions.cs | 3 ++- Bootstrap.DataAccess/Helper/UserHelper.cs | 1 - UnitTest/Bootstrap.DataAccess/LogsTest.cs | 4 ++-- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Bootstrap.Admin/Controllers/AccountController.cs b/Bootstrap.Admin/Controllers/AccountController.cs index 46be098e..850b7499 100644 --- a/Bootstrap.Admin/Controllers/AccountController.cs +++ b/Bootstrap.Admin/Controllers/AccountController.cs @@ -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}"; diff --git a/Bootstrap.Admin/Controllers/Api/LogsController.cs b/Bootstrap.Admin/Controllers/Api/LogsController.cs index ba6cf9c5..60c74cb3 100644 --- a/Bootstrap.Admin/Controllers/Api/LogsController.cs +++ b/Bootstrap.Admin/Controllers/Api/LogsController.cs @@ -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); } } diff --git a/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs b/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs index eb912f13..e8ca286a 100644 --- a/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs +++ b/Bootstrap.Admin/OnlineUsers/OnlineUsersMiddlewareExtensions.cs @@ -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}"; diff --git a/Bootstrap.DataAccess/Helper/UserHelper.cs b/Bootstrap.DataAccess/Helper/UserHelper.cs index d1856096..1ee9da01 100644 --- a/Bootstrap.DataAccess/Helper/UserHelper.cs +++ b/Bootstrap.DataAccess/Helper/UserHelper.cs @@ -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().Authenticate(userName, password); if (ret) loginUser.Result = "登录成功"; LoginHelper.Log(loginUser); diff --git a/UnitTest/Bootstrap.DataAccess/LogsTest.cs b/UnitTest/Bootstrap.DataAccess/LogsTest.cs index 0ec9f7cb..de8bd481 100644 --- a/UnitTest/Bootstrap.DataAccess/LogsTest.cs +++ b/UnitTest/Bootstrap.DataAccess/LogsTest.cs @@ -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" };