本人在建设公司内部的Intranet.为了更好的控制访问后台数据库的用户,除了在网页程序中已完成了对用户名的限制外,另外还打算将访问数据库的客户端计算机名也记录到数据库中, 可是在Baidu.com中搜索了很多的,却没有一个可以达到这个要求. 不成功的帖子或例子如下:
1 Page.Request.UserHostName
2 <%=Request.serverVariables("REMOTE_HOST")&"<br>"%>
3 <script language="JavaScript">
var WshNetwork = new ActiveXObject("WScript.Network");
alert("Domain = " + WshNetwork.UserDomain);
alert("Computer Name = " + WshNetwork.ComputerName);
alert("User Name = " + WshNetwork.UserName);
</script>
不知道那位高手可以解决此问题!谢谢!(菜鸟一个,请所指教!特别是斑竹)
无论是用户名还是IP地址,人家都是可以手动修改的,所以做这样的记录是没有用的,最好的办法是用网卡mac地址,这样除非他换网卡,否则他就走不掉了,嘿嘿。
用下面的js代码就可以获得客户端的IP地址、Mac地址和计算机名, 不过这是用js方式获得的,至于怎么通过js方式获得后传到服务器后台,这就不用我说了吧?
<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
document.forms[0].txtMACAddr.value=unescape(MACAddr);
document.forms[0].txtIPAddr.value=unescape(IPAddr);
document.forms[0].txtDNSName.value=unescape(sDNSName);
</SCRIPT>
<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
{
if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
MACAddr = objObject.MACAddress;
if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
IPAddr = objObject.IPAddress(0);
if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</SCRIPT>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></OBJECT>
<SCRIPT language=JScript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</SCRIPT>
<FORM id=formfoo name=formbar method=post><INPUT name=txtMACAddr> <INPUT name=txtIPAddr> <INPUT value=typ name=txtDNSName>
</FORM></BODY></HTML>
感谢的痛苦流涕了.......
谢谢两位斑竹!搞定!今天我在网上又找了一整天!也找到了很多答案!包括小笨笨帖到上面的Js!(可以运行,呵呵,不过有一个缺陷:不能在windows 9x中运行, 而且在>=2000的OS中,也需要改IE的安全设置,很麻烦!)
我也找了很多关于怎样取得客户端Mac地址的代码例子!不过,都是用C#写的,我不太熟悉!我一般用VB.net
等到我调试成功了,一定放到网上让大家分享!呵呵.....
再次感谢了!对了还有个事情,我说一下: 昨天我注册的是Angel2006,可是今天在登陆时说没有此用户!
不知何原因!没办法,今天又注册了一个!不知道为什么不能在论坛里注册,老报错!我是在博客中注册的!
本人菜鸟一个,没有什么经验!还不知道怎样给你们送币和加分!等我知道了一定给你们加!
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
今天真是高兴呀!问题解决了!我把代码发上来,大家分享!
[CODE]
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Public Class GetMac3
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim strip As String
Dim strmac As String
strip = Page.Request.UserHostAddress
If strip = "127.0.0.1" Then
strip = "192.168.18.41"
End If
Dim dirResults As String
dirResults = ""
Dim psi As ProcessStartInfo = New ProcessStartInfo
Dim proc As Process = New Process
psi.FileName = "nbtstat"
psi.RedirectStandardInput = False
psi.RedirectStandardOutput = True
psi.Arguments = "-A " + strip
psi.UseShellExecute = False
proc = Process.Start(psi)
dirResults = proc.StandardOutput.ReadToEnd()
proc.WaitForExit()
dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "")
'Response.Write(dirResults.ToString())
TextBox1.Text = dirResults
Dim loc1 As Integer
Dim strServer As String
loc1 = dirResults.LastIndexOf("--")
strServer = dirResults.Substring(loc1 + 9, 40)
loc1 = strServer.IndexOf(" ")
strServer = strServer.Substring(0, loc1)
TextBox3.Text = strServer
Dim intstrmaclength As Integer
intstrmaclength = dirResults.Length
strmac = dirResults.Substring(intstrmaclength - 24, 18)
strmac = strmac.Substring(1, 17)
TextBox2.Text = strmac
End Sub
End Class
[/CODE]
搞定!