zl程序教程

您现在的位置是:首页 >  后端

当前栏目

asp.net根据计算机MAC地址限定每台机子只能领取一次账号

NetASP地址计算机Mac 一次 账号 根据
2023-06-13 09:14:34 时间
下面开始吧:
首先写一个简单的前台代码:
复制代码代码如下:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<divstyle="text-align:left">
<strong><spanstyle="font-size:14pt">欢迎光临爱智旮旯的博客!</span><br/>
</strong><spanstyle="font-size:10pt;color:#ff0000">注:每台计算机只可以领取一个帐号<br/>
</span>
<asp:ButtonID="getNamePass"runat="server"OnClick="getNamePass_Click"Text="领取帐号密码"/> <br/>
<asp:LabelID="labName"runat="server"></asp:Label><br/>
<asp:LabelID="labPass"runat="server"></asp:Label><br/>
</div>
</form>
</body>
</html>

再来写一个后台代码,备注已经说的比较清楚,这里不多说了!
复制代码代码如下:

usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Text.RegularExpressions;
usingSystem.Diagnostics;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
labName.Text=labPass.Text="";
}
protectedvoidgetNamePass_Click(objectsender,EventArgse)
{
//获取客户端的IP地址
stringIP=Request.UserHostAddress;
//创建字符串变量
stringdirResults="";
//创建ProcessStartInfo对象表示启动进程时使用的一组值
ProcessStartInfopsi=newProcessStartInfo();
//创建Process对象使您能够启动和停止本地系统进程
Processproc=newProcess();
//设置要启动的应用程序或文档
psi.FileName="nbtstat";
//设置不从Process.StandardInput流中读取输入
psi.RedirectStandardInput=false;
//设置要输出写入Process.StandardOutput流
psi.RedirectStandardOutput=true;
//设置启动的应用程序中的一组命令参数
psi.Arguments="-A"+IP;
//设置从可执行文件创建进程
psi.UseShellExecute=false;
//设置启动进程
proc=Process.Start(psi);
//获取StandardOutput输出流
dirResults=proc.StandardOutput.ReadToEnd();
//设置Process组件无限期地等待关联进程退出
proc.WaitForExit();
//替换掉StandardOutput输出流中的"/r,/n,/t"
dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t","");
//设置正则表达式
Regexreg=newRegex("MAC[]{0,}Address[]{0,}=[]{0,}(?<key>((.)*?))MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
//向获取的StandardOutput输出流添加"MAC"字符串
dirResults=dirResults+"MAC";
//获取Cookie
HttpCookieoldCookie=Request.Cookies["netCard"];
//获取正则表达式中的匹配项
Matchmc=reg.Match(dirResults);
//获取网卡号去除掉“-”符合
stringnetworkCard=mc.Groups["key"].Value.Replace("-","");
//判断Cookie是否为空
if(oldCookie==null)
{
//判断是否符合正则表达式的要求
if(mc.Success)
{
//显示帐号
labName.Text="您的帐号为:"+networkCard;
//显示密码
labPass.Text="您的密码为:1234";
//创建Cookie对象
HttpCookienewCookie=newHttpCookie("netCard");
//设置Cookie的有效时间
newCookie.Expires=DateTime.MaxValue;
//添加Cookie中的值
newCookie.Values.Add("numberCard",networkCard);
//将Cookie添加到Cookie集合中
Response.Cookies.Add(newCookie);
}
else
{
RegisterStartupScript("","<script>alert("您没有联网!");</script>");
}
}
else
{
//获取Cookie中的网卡号
stringnumberCard=oldCookie.Values["numberCard"];
//判断Cookie中的网卡号是否和获取到的网卡号一致
if(numberCard.Trim()==networkCard.Trim())
{
RegisterStartupScript("","<script>alert("很抱歉!您的计算机已领取过帐号。")</script>");
}
else
{
//判断是否符合正则表达式的要求
if(mc.Success)
{
//显示帐号
labName.Text="您的帐号为:"+networkCard;
//显示密码
labPass.Text="您的密码为:1234";
//修改Cookie中的值
oldCookie.Values.Set("numberCard",networkCard);
//将Cookie添加到Cookie集合中
Response.Cookies.Add(oldCookie);
}
else
{
RegisterStartupScript("","<script>alert("您没有联网!");</script>");
}
}
}
}
}