安全Cookie的设计开发C# 第9页

C#安全Cookie的设计开发

4.3 程序设计开发说明

本安全Cookie的设计中用到了一个函数GetClientMac()来获取客户端的MAC地址,若获取MAC地址失败则返回IP地址,文件GetClientMac.cs中该函数主要代码如下:

public class GetClientMac

{

         [DllImport("Iphlpapi.dll")]     //调用Windows IP辅助API应用程序接口

         private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);         //win32API发送ARP报文

         [DllImport("Ws2_32.dll")]       //调用Windows Sockets应用程序接口

         private static extern Int32 inet_addr(string ip);   //ip地址转换

         public GetClientMac(){}

         public static string GetCustomerMac(string userip)

         {   // 实现GetCustomerMac()方法,获取指定ip地址的主机mac地址

             try

             {

            string strClientIP = userip.Trim();

            Int32 ldest = inet_addr(strClientIP); //目的地的ip

            Int32 lhost = inet_addr("");   //本地服务器的ip 若图片无法显示请联系QQ3249114,C#安全Cookie的设计开发系统免费,转发请注明源于www.751com.cn

= 6;

            int res = SendARP(ldest, 0, ref macinfo, ref len);//发送ARP来获取mac地址

            string mac_src = macinfo.ToString("X");   //转换mac地址为16进制表示的字符串

            if (mac_src == "0")

            {

                return userip; //无法获取mac地址则返回ip地址

            }

            else

            {

                while (mac_src.Length < 12)

                {

                    mac_src = mac_src.Insert(0, "0");//mac地址不足12位的在前面加0

                }

                string mac_dest = "";

                for (int i = 0; i < 11; i++) //将源mac转换成常见的每2字符下划线间隔格式

                {

                    if (0 == (i % 2))

                    {

                        if (i == 10)

                        {

                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));

                        }

                      _dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));

                        }

                    }

                }

                return mac_dest;  //返回mac地址

            }

        }

        catch (Exception err)

        {

            return userip;    //出现任何错误返回ip地址

        }

    }

}

 

文件Login.aspx.cs是登陆验证程序的代码,加载页面时使用GetClientMac类中的GetCustomerMac()方法取得客户端MAC或者IP地址。登陆验证使用Membership成员中的ValidateUser()方法验证用户名和密码,通过验证则生成票据写入Cookie并跳转到Default.aspx页面,否则返回登陆页。用户登陆验证代码如下:

 

private string userMac;

    protected void Page_Load(object sender, EventArgs e)

    {

        userMac = GetClientMac.GetCustomerMac(Request.UserHostAddress);

//根据客户端ip取得客户端mac/ip地址

    }

    protected void LoginButton_Click(object sender, EventArgs e)

    {

        string userName = UserBox.Text;  //取得输入的用户名

        string passWord = PwdBox.Text;   //取得输入的密码

        if (Membership.ValidateUser(userName, passWord)) //验证用户身份

        {

            FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddYears(50), false, userMac);

//生成身份验证票据

若图片无法显示请联系QQ3249114,C#安全Cookie的设计开发系统免费,转发请注明源于www.751com.cn);

//对身份验证票据加密

            HttpCookie EncryptCookie = new HttpCookie("EncryptCookie");

            //建立一个Cookie对象

            EncryptCookie.Value = EncryptTicket;

            //将加密后的验证票据赋予Cookie

            if (RemberMe.Checked)     //判断是否需要保留Cookie

            {

                EncryptCookie.Expires = DateTime.Now.AddYears((EncryptCookie);      //Cookie发送到客户端

            Response.Redirect(FormsAuthentication.GetRedirectUrl(userName,false));

            //转到登陆前的页面

        }

        else

        {

            LoginMsg.Text = "登陆失败!用户名或密码错误。"; //用户身份验证失败的提示

        }

}

文件Default.aspx.cs为登陆成功后的信息显示页面代码,此页面打开时将判断Cookie,然后对Cookie进行三重验证,确认Cookie有效后才显示该页,否则返回登陆页。此页面需要使用异常处理模块来防止由于无发获取Cookie而产生的错误。此外,使用了FormsAuthentication.SignOut()注销Cookie。该页面代码如下:

private string userMac;

protected void Page_Load(object sender, EventArgs e)

{

    userMac = GetClientMac.GetCustomerMac(Request.UserHostAddress);

//根据客户端ip取得客户端mac/ip地址

    try

    {

       名为“EncryptCookie”的Cookie的值

       if (userMac == FormsAuthentication.Decrypt(EncryptCookie.Value).UserData)

       {  //将客户端MAC/IPCookie中存储的mac/ip进行对比,从而判断Cookie来源

       MacLabel.Text = userMac;

       Label1.Text += EncryptCookie.Name;

       TextBox1.Text += EncryptCookie.Value;

       Label3.Text += FormsAuthentication.Decrypt(EncryptCookie.Value).Name;

       Label4.Text += FormsAuthentication.Decrypt(EncryptCookie.Value).CookiePath;

                Label5.Text += FormsAuthentication.Decrypt(EncryptCookie. += FormsAuthentication.Decrypt(EncryptCookie.Value).Expired;

                Label7.Text += FormsAuthentication.Decrypt(EncryptCookie.Value).IsPersistent;

                Label8.Text += FormsAuthentication.Decrypt(EncryptCookie.Value).IssueDate;

                Label9.Text += FormsAuthentication.Decrypt(EncryptCookie.Value).UserData;

                Label10.Text += FormsAuthentication.Decrypt(EncryptCookie.Value).Version;

            }

            else

            {     //mac/ip验证失败则执行强制注销操作,并警告提示

                ('MAC/IP验证失败,请重新登陆!');window.location.href='" + FormsAuthentication.LoginUrl + "'</script>");

            }

        }

        catch {   //此页面程序发生任何异常则强制注销并转到登陆页

            FormsAuthentication.SignOut();

            Response.Redirect(FormsAuthentication.LoginUrl);

        }

    }  

4.4 调试经验

在程序开发过程中遇到了一些问题,通过查找资料,咨询老师,都得到了解决。

在程序开发的初始阶段,对ASP.NET内置的身份验证机制不了解,以为需要自己去建立一个用户资料的数据库,后来经过老师的指导,得知可以使用ASP.NET SQL Server安装向导(aspnet_regsql)来创建一个默认成员数据库,从而可以使用ASP.NETForm验证机制。

Web.config中配置数据库连接字符串connectionStrings时,程序运行出现配置错误的提示:项“LocalSqlServer”已添加。对于这个错误,开始有点摸不着头脑,后来

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]  ... 下一页  >> 

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有