How To Get IP Address And MAC Address Using c# , Aps.net MVC
public ActionResult Index()
{
var IP = GetUser_IP();
ViewBag.Ip = IP;
ViewBag.Mac=GetMacAddress(IP);
return View();
}
[NonAction]
protected string GetUser_IP()
{
string VisitorsIPAddr = string.Empty;
if (HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
VisitorsIPAddr =HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Request.UserHostAddress.Length != 0)
{
VisitorsIPAddr = HttpContext.Request.UserHostAddress;
}
return VisitorsIPAddr;
}
[NonAction]
public string GetMacAddress(string ipAddress)
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
IPInterfaceProperties properties = adapter.GetIPProperties();
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
}
return sMacAddress;
}