当前位置:首页 > 操作系统 > MacOs

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

下面开始吧:
首先写一个简单的前台代码:
代码如下:
<BR /><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <BR /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <BR /><html xmlns="http://www.w3.org/1999/xhtml" > <BR /><head runat="server"> <BR /><title>无标题页</title> <BR /></head> <BR /><body> <BR /><form id="form1" runat="server"> <BR /><div style="text-align: left"> <BR /><strong><span style="font-size: 14pt">欢迎光临爱智旮旯的博客!</span><br /> <BR /></strong><span style="font-size: 10pt; color: #ff0000">注:每台计算机只可以领取一个帐号<br /> <BR /></span> <BR /><asp:Button ID="getNamePass" runat="server" OnClick="getNamePass_Click" Text="领取帐号密码" /> <br /> <BR /><asp:Label ID="labName" runat="server"></asp:Label><br /> <BR /><asp:Label ID="labPass" runat="server"></asp:Label><br /> <BR /></div> <BR /></form> <BR /></body> <BR /></html> <BR /> <BR />再来写一个后台代码,备注已经说的比较清楚,这里不多说了! <BR /><span><U></U></span> 代码如下:
<BR />using System; <BR />using System.Data; <BR />using System.Configuration; <BR />using System.Collections; <BR />using System.Web; <BR />using System.Web.Security; <BR />using System.Web.UI; <BR />using System.Web.UI.WebControls; <BR />using System.Web.UI.WebControls.WebParts; <BR />using System.Web.UI.HtmlControls; <BR />using System.Text.RegularExpressions; <BR />using System.Diagnostics; <BR />public partial class _Default : System.Web.UI.Page <BR />{ <BR />protected void Page_Load(object sender, EventArgs e) <BR />{ <BR />labName.Text = labPass.Text = ""; <BR />} <BR />protected void getNamePass_Click(object sender, EventArgs e) <BR />{ <BR />//获取客户端的IP地址 <BR />string IP = Request.UserHostAddress; <BR />//创建字符串变量 <BR />string dirResults = ""; <BR />//创建ProcessStartInfo对象表示启动进程时使用的一组值 <BR />ProcessStartInfo psi = new ProcessStartInfo(); <BR />//创建Process对象使您能够启动和停止本地系统进程 <BR />Process proc = new Process(); <BR />//设置要启动的应用程序或文档 <BR />psi.FileName = "nbtstat"; <BR />//设置不从Process.StandardInput流中读取输入 <BR />psi.RedirectStandardInput = false; <BR />//设置要
输出写入 Process.StandardOutput流 <BR />psi.RedirectStandardOutput = true; <BR />//设置启动的应用程序中的一组命令参数 <BR />psi.Arguments = "-A " + IP; <BR />//设置从可执行文件创建进程 <BR />psi.UseShellExecute = false; <BR />//设置启动进程 <BR />proc = Process.Start(psi); <BR />//获取StandardOutput
输出流
dirResults = proc.StandardOutput.ReadToEnd();
//设置Process 组件无限期地等待关联进程退出
proc.WaitForExit();
//替换掉StandardOutput输出流中的"/r,/n,/t"
dirResults = dirResults.Replace("r", "").Replace("n", "").Replace("t", "");
//设置正则表达式
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"];
//获取正则表达式中的匹配项
Match mc = reg.Match(dirResults);
//获取网卡号去除掉“-”符合
string networkCard = mc.Groups["key"].Value.Replace("-", "");
//判断Cookie是否为空
if (oldCookie == null)
{
//判断是否符合正则表达式的要求
if (mc.Success)
{
//显示帐号
labName.Text = "您的帐号为:" + networkCard;
//显示密码
labPass.Text = "您的密码为:1234";
//创建Cookie对象
HttpCookie newCookie = new HttpCookie("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中的网卡号
string numberCard = 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>");
}
}
}
}
}

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐