.net通过获取客户端IP地址反查出用户的计算机名
這個功能看似很少用到,但又非常實用,看似簡單,但又在其中存在很多未知因素造成讓人悲痛莫名的負能量。。。
這是公司內部最近在使用的功能,因為是DHCP,所以有時會根據計算機名做一些統計和權限的設置。
也許你已經通過廢柴度或時常打不開哥,搜索到了一堆代碼,基本上全是長的一樣的玩意兒。通過試驗,基本上只能獲取本機信息或服務器信息,亦或上傳到服務器上啥信息也獲取不到了,就在我原以為用IP反查計算機名成功的時候,卻發現內部DNS雙向解析有問題,經常是PING計算機名得到IP,但PING這個IP得到的是另一個計算機名。。。。真是讓人仰天悲鳴。
要求:內部網站某一個菜單只允許規定的域用戶訪問,不能用登錄窗口,類似用戶名密碼這樣的東西。
解決方法:用戶點擊菜單,得到IP,用NBTSTAT -A IP得到客戶端的計算機名,在后臺程序中用正則過濾出計算機名進行判斷。
下面是部分代碼和解決方法,變通還是很重要的,細節不是重點,重點是想法,當然你可以把代碼寫的更漂亮高效,^^。
你可以新建個頁面,在后臺寫上下面的代碼。
? ? ? ? if (!IsPostBack)
? ? ? ?{
? ?string?strClientHostname = GetHostname(GetIP());
? ? ? ? ? ?if (strClientHostname == "china-2021-k90"? ?|| ?strClientHostname == "china-hjbai"? ?)
? ? ? ? ? ?{
? ? ? ? ? ? ? ?bind();//輸出結果
? ? ? ? ? ?}
? ? ? ? ? ?else
? ? ? ? ? ?{
? ? ? ? ? ? ? ?Page.ClientScript.RegisterStartupScript(this.GetType(), "Warning", "<script lanuage=javascript>dialog_show(); </script>");
? ? ? ? ? ?}
? ? ? ?}
? ? //得到客戶端IP
? ? public string GetIP()
? ? {
? ? ? ? string uip = "";
? ? ? ? if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
? ? ? ? {
? ? ? ? ? ? uip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? uip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
? ? ? ? }
? ? ? ? return uip;
? ? }
? ? public string GetHostname(string IP)?
? ? {
? ? ? ? string dirResults = "";
? ? ? ? ProcessStartInfo psi = new ProcessStartInfo();
? ? ? ? Process proc = new Process();
//這里聰明的你會想到很多命令的有趣用法吧
? ? ? ? //psi.FileName = "ping ";
? ? ? ? //psi.RedirectStandardInput = false;
? ? ? ? //psi.RedirectStandardOutput = true;
? ? ? ? //psi.Arguments = "-a -n 1 " + IP;
? ? ? ? psi.FileName = "nbtstat ";
? ? ? ? psi.RedirectStandardInput = false;
? ? ? ? psi.RedirectStandardOutput = true;
? ? ? ? psi.Arguments = "-A " + IP;
? ? ? ?//這里對結果進行正則過濾,你可以在CMD窗口運行DOS命令看下結果,這樣會更明了
? ? ? ? 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("china-(?:[a-z][a-z0-9_]*)*", RegexOptions.IgnoreCase | RegexOptions.Compiled);
? ? ? ? dirResults = dirResults.ToLower();
? ? ? ? Match mc = reg.Match(dirResults);
? ? ? ? //Response.Write(dirResults.ToLower());
? ? ? ? if (mc.Success)
? ? ? ? {
? ? ? ? ? ? return mc.ToString();
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? //這個是正則的另一種拼接方法,因為有些計算機名比較特殊
? ? ? ? ? ? string re1 = "(china)";// Word 1
? ? ? ? ? ? string re2 = "([-+]\\d+)";// Integer Number 1
? ? ? ? ? ? string re3 = "(-)";// Any Single Character 1
? ? ? ? ? ? string re4 = "((?:[a-z][a-z0-9_]*))"; // Variable Name 1
? ? ? ? ? ? Regex r = new Regex(re1 + re2 + re3 + re4, RegexOptions.IgnoreCase | RegexOptions.Singleline);
? ? ? ? ? ? Match mc2 = r.Match(dirResults.ToLower());
? ? ? ? ? ? if (mc2.Success)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return mc2.ToString();
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? reg = new Regex("Host not found", RegexOptions.IgnoreCase | RegexOptions.Compiled);
? ? ? ? ? ? ? ? mc = reg.Match(dirResults);
? ? ? ? ? ? ? ? if (mc.Success)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return "Host not found!";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return "";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
//
//希望能推薦到首頁哎,雖然簡單,但想必對很多人還是能起到幫助的...
?
總結
以上是生活随笔為你收集整理的.net通过获取客户端IP地址反查出用户的计算机名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库-锁的实践
- 下一篇: EXCEL打开CSV文件乱码的解决方法