asp.net根據(jù)計(jì)算機(jī)MAC地址限定每臺(tái)機(jī)子只能領(lǐng)取一次賬號(hào)
更新時(shí)間:2012年06月06日 22:45:55 作者:
這里只做簡(jiǎn)單演示過(guò)程,請(qǐng)根據(jù)您的實(shí)際情況作適當(dāng)修改!另外我的博客只做自己參考查詢方便用,請(qǐng)各位大神不要沒(méi)事噴我,知道您的技術(shù)高,我是新手正在努力學(xué)習(xí)當(dāng)中,謝謝
下面開始吧:
首先寫一個(gè)簡(jiǎn)單的前臺(tái)代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: left">
<strong><span style="font-size: 14pt">歡迎光臨愛(ài)智旮旯的博客!</span><br />
</strong><span style="font-size: 10pt; color: #ff0000">注:每臺(tái)計(jì)算機(jī)只可以領(lǐng)取一個(gè)帳號(hào)<br />
</span>
<asp:Button ID="getNamePass" runat="server" OnClick="getNamePass_Click" Text="領(lǐng)取帳號(hào)密碼" /> <br />
<asp:Label ID="labName" runat="server"></asp:Label><br />
<asp:Label ID="labPass" runat="server"></asp:Label><br />
</div>
</form>
</body>
</html>
再來(lái)寫一個(gè)后臺(tái)代碼,備注已經(jīng)說(shuō)的比較清楚,這里不多說(shuō)了!
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
labName.Text = labPass.Text = "";
}
protected void getNamePass_Click(object sender, EventArgs e)
{
//獲取客戶端的IP地址
string IP = Request.UserHostAddress;
//創(chuàng)建字符串變量
string dirResults = "";
//創(chuàng)建ProcessStartInfo對(duì)象表示啟動(dòng)進(jìn)程時(shí)使用的一組值
ProcessStartInfo psi = new ProcessStartInfo();
//創(chuàng)建Process對(duì)象使您能夠啟動(dòng)和停止本地系統(tǒng)進(jìn)程
Process proc = new Process();
//設(shè)置要啟動(dòng)的應(yīng)用程序或文檔
psi.FileName = "nbtstat";
//設(shè)置不從Process.StandardInput流中讀取輸入
psi.RedirectStandardInput = false;
//設(shè)置要輸出寫入 Process.StandardOutput流
psi.RedirectStandardOutput = true;
//設(shè)置啟動(dòng)的應(yīng)用程序中的一組命令參數(shù)
psi.Arguments = "-A " + IP;
//設(shè)置從可執(zhí)行文件創(chuàng)建進(jìn)程
psi.UseShellExecute = false;
//設(shè)置啟動(dòng)進(jìn)程
proc = Process.Start(psi);
//獲取StandardOutput輸出流
dirResults = proc.StandardOutput.ReadToEnd();
//設(shè)置Process 組件無(wú)限期地等待關(guān)聯(lián)進(jìn)程退出
proc.WaitForExit();
//替換掉StandardOutput輸出流中的"/r,/n,/t"
dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "");
//設(shè)置正則表達(dá)式
Regex reg = new Regex("MAC[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))MAC", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//向獲取的StandardOutput輸出流添加"MAC"字符串
dirResults = dirResults + "MAC";
//獲取Cookie
HttpCookie oldCookie = Request.Cookies["netCard"];
//獲取正則表達(dá)式中的匹配項(xiàng)
Match mc = reg.Match(dirResults);
//獲取網(wǎng)卡號(hào)去除掉“-”符合
string networkCard = mc.Groups["key"].Value.Replace("-", "");
//判斷Cookie是否為空
if (oldCookie == null)
{
//判斷是否符合正則表達(dá)式的要求
if (mc.Success)
{
//顯示帳號(hào)
labName.Text = "您的帳號(hào)為:" + networkCard;
//顯示密碼
labPass.Text = "您的密碼為:1234";
//創(chuàng)建Cookie對(duì)象
HttpCookie newCookie = new HttpCookie("netCard");
//設(shè)置Cookie的有效時(shí)間
newCookie.Expires = DateTime.MaxValue;
//添加Cookie中的值
newCookie.Values.Add("numberCard", networkCard);
//將Cookie添加到Cookie集合中
Response.Cookies.Add(newCookie);
}
else
{
RegisterStartupScript("", "<script>alert( '您沒(méi)有聯(lián)網(wǎng)!');</script>");
}
}
else
{
//獲取Cookie中的網(wǎng)卡號(hào)
string numberCard = oldCookie.Values["numberCard"];
//判斷Cookie中的網(wǎng)卡號(hào)是否和獲取到的網(wǎng)卡號(hào)一致
if (numberCard.Trim() == networkCard.Trim())
{
RegisterStartupScript("", "<script>alert('很抱歉!您的計(jì)算機(jī)已領(lǐng)取過(guò)帳號(hào)。')</script>");
}
else
{
//判斷是否符合正則表達(dá)式的要求
if (mc.Success)
{
//顯示帳號(hào)
labName.Text = "您的帳號(hào)為:" + networkCard;
//顯示密碼
labPass.Text = "您的密碼為:1234";
//修改Cookie中的值
oldCookie.Values.Set("numberCard", networkCard);
//將Cookie添加到Cookie集合中
Response.Cookies.Add(oldCookie);
}
else
{
RegisterStartupScript("", "<script>alert( '您沒(méi)有聯(lián)網(wǎng)!');</script>");
}
}
}
}
}
首先寫一個(gè)簡(jiǎn)單的前臺(tái)代碼:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: left">
<strong><span style="font-size: 14pt">歡迎光臨愛(ài)智旮旯的博客!</span><br />
</strong><span style="font-size: 10pt; color: #ff0000">注:每臺(tái)計(jì)算機(jī)只可以領(lǐng)取一個(gè)帳號(hào)<br />
</span>
<asp:Button ID="getNamePass" runat="server" OnClick="getNamePass_Click" Text="領(lǐng)取帳號(hào)密碼" /> <br />
<asp:Label ID="labName" runat="server"></asp:Label><br />
<asp:Label ID="labPass" runat="server"></asp:Label><br />
</div>
</form>
</body>
</html>
再來(lái)寫一個(gè)后臺(tái)代碼,備注已經(jīng)說(shuō)的比較清楚,這里不多說(shuō)了!
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
labName.Text = labPass.Text = "";
}
protected void getNamePass_Click(object sender, EventArgs e)
{
//獲取客戶端的IP地址
string IP = Request.UserHostAddress;
//創(chuàng)建字符串變量
string dirResults = "";
//創(chuàng)建ProcessStartInfo對(duì)象表示啟動(dòng)進(jìn)程時(shí)使用的一組值
ProcessStartInfo psi = new ProcessStartInfo();
//創(chuàng)建Process對(duì)象使您能夠啟動(dòng)和停止本地系統(tǒng)進(jìn)程
Process proc = new Process();
//設(shè)置要啟動(dòng)的應(yīng)用程序或文檔
psi.FileName = "nbtstat";
//設(shè)置不從Process.StandardInput流中讀取輸入
psi.RedirectStandardInput = false;
//設(shè)置要輸出寫入 Process.StandardOutput流
psi.RedirectStandardOutput = true;
//設(shè)置啟動(dòng)的應(yīng)用程序中的一組命令參數(shù)
psi.Arguments = "-A " + IP;
//設(shè)置從可執(zhí)行文件創(chuàng)建進(jìn)程
psi.UseShellExecute = false;
//設(shè)置啟動(dòng)進(jìn)程
proc = Process.Start(psi);
//獲取StandardOutput輸出流
dirResults = proc.StandardOutput.ReadToEnd();
//設(shè)置Process 組件無(wú)限期地等待關(guān)聯(lián)進(jìn)程退出
proc.WaitForExit();
//替換掉StandardOutput輸出流中的"/r,/n,/t"
dirResults = dirResults.Replace("\r", "").Replace("\n", "").Replace("\t", "");
//設(shè)置正則表達(dá)式
Regex reg = new Regex("MAC[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))MAC", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//向獲取的StandardOutput輸出流添加"MAC"字符串
dirResults = dirResults + "MAC";
//獲取Cookie
HttpCookie oldCookie = Request.Cookies["netCard"];
//獲取正則表達(dá)式中的匹配項(xiàng)
Match mc = reg.Match(dirResults);
//獲取網(wǎng)卡號(hào)去除掉“-”符合
string networkCard = mc.Groups["key"].Value.Replace("-", "");
//判斷Cookie是否為空
if (oldCookie == null)
{
//判斷是否符合正則表達(dá)式的要求
if (mc.Success)
{
//顯示帳號(hào)
labName.Text = "您的帳號(hào)為:" + networkCard;
//顯示密碼
labPass.Text = "您的密碼為:1234";
//創(chuàng)建Cookie對(duì)象
HttpCookie newCookie = new HttpCookie("netCard");
//設(shè)置Cookie的有效時(shí)間
newCookie.Expires = DateTime.MaxValue;
//添加Cookie中的值
newCookie.Values.Add("numberCard", networkCard);
//將Cookie添加到Cookie集合中
Response.Cookies.Add(newCookie);
}
else
{
RegisterStartupScript("", "<script>alert( '您沒(méi)有聯(lián)網(wǎng)!');</script>");
}
}
else
{
//獲取Cookie中的網(wǎng)卡號(hào)
string numberCard = oldCookie.Values["numberCard"];
//判斷Cookie中的網(wǎng)卡號(hào)是否和獲取到的網(wǎng)卡號(hào)一致
if (numberCard.Trim() == networkCard.Trim())
{
RegisterStartupScript("", "<script>alert('很抱歉!您的計(jì)算機(jī)已領(lǐng)取過(guò)帳號(hào)。')</script>");
}
else
{
//判斷是否符合正則表達(dá)式的要求
if (mc.Success)
{
//顯示帳號(hào)
labName.Text = "您的帳號(hào)為:" + networkCard;
//顯示密碼
labPass.Text = "您的密碼為:1234";
//修改Cookie中的值
oldCookie.Values.Set("numberCard", networkCard);
//將Cookie添加到Cookie集合中
Response.Cookies.Add(oldCookie);
}
else
{
RegisterStartupScript("", "<script>alert( '您沒(méi)有聯(lián)網(wǎng)!');</script>");
}
}
}
}
}
您可能感興趣的文章:
- ASP.NET MVC5驗(yàn)證系列之服務(wù)端驗(yàn)證
- ASP.NET MVC+EF在服務(wù)端分頁(yè)使用jqGrid以及jquery Datatables的注意事項(xiàng)
- ASP.NET獲取真正的客戶端IP地址的6種方法
- asp.net 從客戶端中檢測(cè)到有潛在危險(xiǎn)的 Request.Form 值錯(cuò)誤解
- asp.net(C#)中給控件添加客戶端js事件的方法
- asp.net 客戶端瀏覽器緩存的Http頭介紹
- 設(shè)置ASP.NET頁(yè)面不被緩存(客戶端/服務(wù)器端取消緩存方法)
- ASP.net中獲取客戶端參數(shù)操作系統(tǒng)信息
- asp.net實(shí)現(xiàn)獲取客戶端詳細(xì)信息
- ASP.NET簡(jiǎn)單獲取服務(wù)端和客戶端計(jì)算機(jī)名稱的方法
相關(guān)文章
通過(guò)ASP.net實(shí)現(xiàn)flash對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)
近來(lái)網(wǎng)站需要在flash中提取數(shù)據(jù)庫(kù)中的數(shù)據(jù),從網(wǎng)上找了一點(diǎn)資料,今天下午在自己的機(jī)器上實(shí)現(xiàn)了一下,還是比較簡(jiǎn)單的。2009-08-08AjaxControlToolKit 顯示瀏覽者本地語(yǔ)言的方法
使用最新版的AjaxControlToolKit控件2008-12-12ASP.NET?MVC5網(wǎng)站開發(fā)咨詢管理的架構(gòu)(十一)
這篇文章主要介紹了ASP.NET?MVC5網(wǎng)站開發(fā)咨詢管理的架構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09asp.net利用存儲(chǔ)過(guò)程和div+css實(shí)現(xiàn)分頁(yè)(類似于博客園首頁(yè)分頁(yè))
怎么用aspnetpager.dll這個(gè)插件對(duì)服務(wù)器控件進(jìn)行分頁(yè),今天與我大家分享一下asp.net利用存儲(chǔ)過(guò)程和div+css實(shí)現(xiàn)分頁(yè)(類似于博客園首頁(yè)分頁(yè))2012-01-01asp.net開發(fā)微信派發(fā)現(xiàn)金紅包/H5網(wǎng)頁(yè)搶紅包功能(思路詳解)
這篇文章主要介紹了asp.net開發(fā)微信派發(fā)現(xiàn)金紅包/H5網(wǎng)頁(yè)搶紅包功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03