根據電腦MAC地址限定每台機子只能領取一次帳號

來源:互聯網
上載者:User

這裡只做簡單示範過程,請根據您的實際情況作適當修改!

另外我的部落格只做自己參考查詢方便用,請各位大神不要沒事噴我,知道您的技術高,我是新手正在努力學習當中,謝謝!!!!

下面開始吧:

首先寫一個簡單的前台代碼:

<%@ 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="領取帳號密碼" />&nbsp;<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>");                    }                }            }              }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.