如何在ASP.NET Core中获取客户端计算机名称

4

我已经尝试过

System.Net.Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress.ToString()).ToString();

但是那并没有起作用。 我还尝试了使用。
PCName = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName; 

但是这似乎已经被弃用了Core。

2
也许可以参考这个 - Grizzly
@M12Bennett 我之前也看过那个页面,但似乎找不到这些方法的命名空间。 - user9909112
你指的是哪个方法? - Grizzly
@M12Bennett Request.UserHostName; comppNameHelper; 还有IPAddress和IPHostEntry我在我的控制器中包含了以下命名空间using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using MyModel; using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using System.Web; - user9909112
在ASP.Net Core中,“Request.UserHostName”是“Request.HttpContext.Connection.RemoteIpAddress”。 “CompNameHelper”是静态类,其中包含了“DetermineCompName”方法。您需要根据放置位置调用它。值得注意的是,除非客户端参与,否则没有任何方法可能会为您提供客户端主机名,除非客户端在局域网上。 - ProgrammingLlama
显示剩余5条评论
2个回答

6

试一试。

using Microsoft.AspNetCore.Http;
using System.Net;

Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress).HostName;

0
httpContext.Features.Get<IServerVariablesFeature>()["REMOTE_HOST"]

仅适用于Windows,因为请查看此SO答案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接