下面開始吧:
首先寫一個簡單的前台代碼:
複製代碼 代碼如下:<%@ 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
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"];
//擷取Regex中的匹配項
Match mc = reg.Match(dirResults);
//擷取網卡號去除掉“-”符合
string networkCard = mc.Groups["key"].Value.Replace("-", "");
//判斷Cookie是否為空白
if (oldCookie == null)
{
//判斷是否符合Regex的要求
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
{
//判斷是否符合Regex的要求
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>");
}
}
}
}
}