欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 19:06:37
下面开始吧: 首先写一个简单的前台代码: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codef...
下面开始吧:
首先写一个简单的前台代码:
复制代码 代码如下:

<%@ 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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: left">
<strong><span style="font-size: 14pt">欢迎光临爱智旮旯的博客!</span><br />
</strong><span style="font-size: 10pt; color: #ff0000">注:每台计算机只可以领取一个帐号<br />
</span>
<asp:button id="getnamepass" runat="server" onclick="getnamepass_click" text="领取帐号密码" /> <br />
<asp:label id="labname" runat="server"></asp:label><br />
<asp:label id="labpass" runat="server"></asp:label><br />
</div>
</form>
</body>
</html>

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

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;
//创建字符串变量
string dirresults = "";
//创建processstartinfo对象表示启动进程时使用的一组值
processstartinfo psi = new processstartinfo();
//创建process对象使您能够启动和停止本地系统进程
process proc = new process();
//设置要启动的应用程序或文档
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", "");
//设置正则表达式
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>");
}
}
}
}
}