日期:2014-05-20  浏览次数:20457 次

[100分]如何:asp.net下读取客户端mac地址(注:为b/s结构,非c/s结构)
如题!!

------解决方案--------------------
试一试如下代码,一个类文件:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;

namespace BobomouseComponent
{
/// <summary>
/// MAC 的摘要说明。
/// </summary>
public class Mac : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
[DllImport( "Iphlpapi.dll ")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport( "Ws2_32.dll ")]
private static extern Int32 inet_addr(string ip);
static TimeSpan waitTime=new TimeSpan(0,0,0,3);

public string GetMacAddress(string ipAddress) // 得到指定IP的MAC地址
{
Int32 ldest=inet_addr(ipAddress);
Int64 macinfo=new Int64();
Int32 len=6;
try
{
int res=SendARP(ldest,0,ref macinfo,ref len);
}
catch(Exception err)
{
throw new Exception( "在解析MAC地址过程发生了错误! ");
}
string originalMACAddress=macinfo.ToString( "X4 ");
return originalMACAddress;
}
}
}
======================
或者试一试下面的代码,用ASP.Net获取客户端网卡的MAC 和IP

uisng System.Text.RegularExpressions;
using System.Diagnostics;

public class test
{
public test
{}
public static string GetCustomerMac(string IP) //para IP is the client 's IP
{
string dirResults= " ";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat ";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-A " + IP;
psi.UseShellExecute = false;
proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
dirResults=dirResults.Replace( "\r ", " ").Replace( "\n ", " ").Replace( "\t ", " ");

Regex reg=new Regex( "Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(? <key> ((.)*?)) __MAC ",RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match mc=reg.Match(dirResults+ "__MAC ");

if(mc.Success)
{
return mc.Groups[ "key "].Value;
}
else
{
reg=new Regex( "Host not found ",RegexOptions.IgnoreCase|RegexOptions.Compiled);
mc=reg.Match(dirResults);
if(mc.Success)
{
return "Host not found! ";
}
else
{
return " ";
}
}
}
}

//IP Response.Write( " <br> IP= "+Request.UserHostAddress.ToString());



------解决方案--------------------
比较难的哦,呵呵。
服务端脚本和客户端脚本是执行在不同的地方了。