我试图获取当前使用的网络适配器。 例如,如果我在ASP.NET Core中收到Web请求,我想知道正在使用哪个适配器来处理该请求。
谢谢!
答案 0 :(得分:1)
您可以使用HttpContext.Connection.LocalIpAddress
property获取连接本地(服务器)端的IP地址,然后找到绑定到该地址的NetworkInterface
实例。
using System.Linq;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public IActionResult GetInterfaceInfo()
{
var connectionLocalAddress = HttpContext.Connection.LocalIpAddress;
var connectionLocalInterface = NetworkInterface.GetAllNetworkInterfaces()
.Where(iface => iface.GetIPProperties().UnicastAddresses.Any(unicastInfo => unicastInfo.Address.Equals(connectionLocalAddress)))
.SingleOrDefault();
var results = new {
NetworkInterface = connectionLocalInterface,
IPInterfaceProperties = connectionLocalInterface?.GetIPProperties(),
IPInterfaceStatistics = connectionLocalInterface?.GetIPStatistics(),
IPv4InterfaceStatistics = connectionLocalInterface?.GetIPv4Statistics()
};
return Json(
results,
new JsonSerializerSettings() {
ContractResolver = new IgnorePropertyExceptionsResolver(),
Formatting = Formatting.Indented
}
);
}
}
}
访问http://localhost:12345/Home/GetInterfaceInfo
然后生成......
{
"NetworkInterface": {
"Id": "{01234567-ABCD-EF01-2345-6789ABCDEF01}",
"Name": "Loopback Pseudo-Interface 1",
"Description": "Software Loopback Interface 1",
"NetworkInterfaceType": 24,
"OperationalStatus": 1,
"Speed": 1073741824,
"IsReceiveOnly": false,
"SupportsMulticast": true
},
"IPInterfaceProperties": {
"IsDnsEnabled": false,
"IsDynamicDnsEnabled": true,
"DnsSuffix": "",
"AnycastAddresses": [],
"UnicastAddresses": [
{
"Address": {
"AddressFamily": 23,
"ScopeId": 0,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": false,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false
},
"IPv4Mask": {
"AddressFamily": 2,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": false,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false,
"Address": 0
},
"PrefixLength": 128,
"IsTransient": false,
"IsDnsEligible": false,
"PrefixOrigin": 2,
"SuffixOrigin": 2,
"DuplicateAddressDetectionState": 4,
"AddressValidLifetime": 4294967295,
"AddressPreferredLifetime": 4294967295,
"DhcpLeaseLifetime": 1542939
},
{
"Address": {
"AddressFamily": 2,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": false,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false,
"Address": 16777343
},
"IPv4Mask": {
"AddressFamily": 2,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": false,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false,
"Address": 255
},
"PrefixLength": 8,
"IsTransient": false,
"IsDnsEligible": false,
"PrefixOrigin": 2,
"SuffixOrigin": 2,
"DuplicateAddressDetectionState": 4,
"AddressValidLifetime": 4294967295,
"AddressPreferredLifetime": 4294967295,
"DhcpLeaseLifetime": 1542939
}
],
"MulticastAddresses": [
{
"Address": {
"AddressFamily": 23,
"ScopeId": 1,
"IsIPv6Multicast": true,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": false,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false
},
"IsTransient": false,
"IsDnsEligible": false,
"PrefixOrigin": 0,
"SuffixOrigin": 0,
"DuplicateAddressDetectionState": 0,
"AddressValidLifetime": 0,
"AddressPreferredLifetime": 0,
"DhcpLeaseLifetime": 0
},
{
"Address": {
"AddressFamily": 2,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": false,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false,
"Address": 4211081199
},
"IsTransient": false,
"IsDnsEligible": false,
"PrefixOrigin": 0,
"SuffixOrigin": 0,
"DuplicateAddressDetectionState": 0,
"AddressValidLifetime": 0,
"AddressPreferredLifetime": 0,
"DhcpLeaseLifetime": 0
}
],
"DnsAddresses": [
{
"AddressFamily": 23,
"ScopeId": 1,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": true,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false
},
{
"AddressFamily": 23,
"ScopeId": 1,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": true,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false
},
{
"AddressFamily": 23,
"ScopeId": 1,
"IsIPv6Multicast": false,
"IsIPv6LinkLocal": false,
"IsIPv6SiteLocal": true,
"IsIPv6Teredo": false,
"IsIPv4MappedToIPv6": false
}
],
"GatewayAddresses": [],
"DhcpServerAddresses": [],
"WinsServersAddresses": []
},
"IPInterfaceStatistics": {
"OutputQueueLength": 0,
"BytesSent": 0,
"BytesReceived": 0,
"UnicastPacketsSent": 0,
"UnicastPacketsReceived": 0,
"NonUnicastPacketsSent": 0,
"NonUnicastPacketsReceived": 0,
"IncomingPacketsDiscarded": 0,
"OutgoingPacketsDiscarded": 0,
"IncomingPacketsWithErrors": 0,
"OutgoingPacketsWithErrors": 0,
"IncomingUnknownProtocolPackets": 0
},
"IPv4InterfaceStatistics": {
"OutputQueueLength": 0,
"BytesSent": 0,
"BytesReceived": 0,
"UnicastPacketsSent": 0,
"UnicastPacketsReceived": 0,
"NonUnicastPacketsSent": 0,
"NonUnicastPacketsReceived": 0,
"IncomingPacketsDiscarded": 0,
"OutgoingPacketsDiscarded": 0,
"IncomingPacketsWithErrors": 0,
"OutgoingPacketsWithErrors": 0,
"IncomingUnknownProtocolPackets": 0
}
}
IgnorePropertyExceptionsResolver
课程来自Ignoring class members that throw exceptions when serializing to JSON,看起来像这样......
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace WebApplication1.Controllers
{
internal class IgnorePropertyExceptionsResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jsonProperty = base.CreateProperty(member, memberSerialization);
jsonProperty.ShouldSerialize = instance => {
try
{
var instanceProperty = (PropertyInfo) member;
if (instanceProperty.CanRead)
{
instanceProperty.GetValue(instance, null);
return true;
}
}
catch
{
}
return false;
};
return jsonProperty;
}
}
}
我使用此类的原因是因为IPAddress
的集合属性中的某些results.IPInterfaceProperties
对象在访问其Address
属性时会抛出异常,因此解析器只是省略使输出有效的违规属性。当然,您将直接访问这些对象,而不是首先将它们转换为JSON,因此该类仅对此演示代码非常需要。