📄 left.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hugo;
namespace BookShop
{
/// <summary>
/// Left 的摘要说明。
/// </summary>
public class Left : System.Web.UI.Page
{
BookType myBookType =new BookType();
protected System.Web.UI.WebControls.TextBox tb_Password;
protected System.Web.UI.WebControls.TextBox tb_Account;
protected System.Web.UI.WebControls.DataGrid dg_BookType;
protected System.Web.UI.WebControls.DataGrid dg_Book10;
Book myBook=new Book ();
private int id;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.ImageButton ib_Login;
protected System.Web.UI.WebControls.CheckBox chkRemember;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.HyperLink HyperLink2;
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Label lblDate;
protected System.Web.UI.WebControls.Label lblTimes;
protected System.Web.UI.WebControls.Label lblInfo;
protected System.Web.UI.WebControls.HyperLink HyperLink3;
protected System.Web.UI.WebControls.HyperLink HyperLink4;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label lblInfo2;
protected System.Web.UI.WebControls.HyperLink hlCity;
private string name;
private void Page_Load(object sender, System.EventArgs e)
{
if (!(this.IsPostBack))
{
if(!Object.Equals(Session["UserName"],null))
{
ShowInfo();
}
else
{
if(!Object.Equals(Request.Cookies["HugoShopBookLogin"],null))
{
string username;
GetCookie(out username);
this.tb_Account.Text = username;
}
}
GridBind();
}
}
private void GridBind()
{
DataView MyDv1;
DataView MyDv2;
MyDv1=myBookType.GetBookType();
dg_BookType.DataSource =MyDv1;
dg_BookType.DataBind();
MyDv2=myBook.GetBookTop10();
dg_Book10.DataSource=MyDv2;
dg_Book10.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ib_Login.Click += new System.Web.UI.ImageClickEventHandler(this.ib_Login_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ib_Login_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Hugo.BookShop.User user = new Hugo.BookShop.User();
name = this.tb_Account.Text.Trim();
user.Name = name;
user.Password = this.tb_Password.Text.Trim();
if(user.Login())
{
this.lblInfo2.Text = "Welcome,"+user.Name;
try
{
user.GetUserInfo();
id = user.ID;
Session["UserName"] = name;
Session["UserID"] = id;
if(Object.Equals(Request.Cookies["HugoShopBookLogin"],null))
{
CreateCookie();
ShowInfo();
}
else
{
ShowInfo();
UpdateCookie();
}
Page.RegisterStartupScript("refresh","<script language=javascript>parent.top.history.go(0);</script>");
}
catch(Exception ex)
{
this.lblInfo.Text = "Get your information FAILED!" + ex.Message;
}
}
else
{
this.lblInfo.Text = "Login FAILED!";
}
}
#region Functions for manipulating cookie
/// <summary>
/// Create new cookie named HugoShopBookLogin
/// </summary>
private void CreateCookie()
{
HttpCookie cookie = new HttpCookie("HugoShopBookLogin");
string now = DateTime.Now.ToString();
string ip = Request.UserHostAddress;
// if(this.chkRemember.Checked)
// {
// cookie.Values.Add("Password",Functions.DecryptCookie(this.tb_Password.Text));
// }
//Set the sum value of all the cookies to "Value"
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(name);
sb.Append("#");
sb.Append(id.ToString());
sb.Append("#");
sb.Append(now);
sb.Append("#");
sb.Append("1");
sb.Append("#");
sb.Append(ip);
int seal = Sum(sb.ToString());
sb.Append("#");
sb.Append(seal.ToString());
Random rnd = new Random();
int type = rnd.Next(0,9999);
cookie.Values.Add("Value",type.ToString());
cookie.Values.Add("BookShop",Functions.EncryptCookie(sb.ToString(),type));
cookie.Expires = DateTime.MaxValue;
Response.AppendCookie(cookie);
}
/// <summary>
/// Get cookie
/// </summary>
/// <param name="name">return user name</param>
private void GetCookie(out string name)
{
HttpCookie cookie = Request.Cookies["HugoShopBookLogin"];
int type = int.Parse(cookie.Values["Value"].ToString());
string bookShopCookie = Functions.DecryptCookie(cookie.Values["BookShop"].ToString(),type);
string [] myCookie = new String[10];
myCookie = bookShopCookie.Split('#');
name = myCookie[0];
string id = myCookie[1];
string now = myCookie[2];
string times = myCookie[3];
string ip = myCookie[4];
string seal = myCookie[5];
if(Request.UserHostAddress != ip)
{
name = "";
return;
}
//check the sum value
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(name);
sb.Append("#");
sb.Append(id.ToString());
sb.Append("#");
sb.Append(now);
sb.Append("#");
sb.Append(times);
sb.Append("#");
sb.Append(ip);
if(Sum(sb.ToString()) != int.Parse(seal))
{
name = "";
}
return;
}
/// <summary>
/// Get city
/// </summary>
/// <returns>city name</returns>
private string GetCityCookie()
{
try
{
HttpCookie cookie = Request.Cookies["HugoShopBookCity"];
return cookie.Values["City"].ToString();
}
catch
{
return "";
}
}
/// <summary>
/// Get the last visitted time and visitted times
/// </summary>
/// <param name="lastTime"></param>
/// <param name="times"></param>
private void GetVisitInfo(out string lastTime,out string times)
{
HttpCookie cookie = Request.Cookies["HugoShopBookLogin"];
int type = int.Parse(cookie.Values["Value"].ToString());
string bookShopCookie = Functions.DecryptCookie(cookie.Values["BookShop"].ToString(),type);
string [] myCookie = new String[10];
myCookie = bookShopCookie.Split('#');
lastTime = myCookie[2];
times = myCookie[3];
}
/// <summary>
/// Update cookie
/// </summary>
private void UpdateCookie()
{
HttpCookie cookie = new HttpCookie("HugoShopBookLogin");
HttpCookie oldCookie = Request.Cookies["HugoShopBookLogin"];
int type = int.Parse(oldCookie.Values["Value"].ToString());
string bookShopCookie = Functions.DecryptCookie(oldCookie.Values["BookShop"].ToString(),type);
string now = DateTime.Now.ToString();
string ip = Request.UserHostAddress;
// if(this.chkRemember.Checked)
// {
// cookie.Values.Add("Password",Functions.DecryptCookie(this.tb_Password.Text));
// }
//Set the sum value of all the cookies to "Value"
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(name);
sb.Append("#");
sb.Append(id.ToString());
sb.Append("#");
sb.Append(now);
sb.Append("#");
string [] myCookie = new String[10];
myCookie = bookShopCookie.Split('#');
int i = int.Parse(myCookie[3])+1;
sb.Append(i.ToString());
sb.Append("#");
sb.Append(ip);
int seal = Sum(sb.ToString());
sb.Append("#");
sb.Append(seal.ToString());
Random rnd = new Random();
type = rnd.Next(0,9999);
cookie.Values.Add("Value",type.ToString());
cookie.Values.Add("BookShop",Functions.EncryptCookie(sb.ToString(),type));
cookie.Expires = DateTime.MaxValue;
Response.AppendCookie(cookie);
}
/// <summary>
/// Show visit information
/// </summary>
private void ShowInfo()
{
this.Panel1.Visible = false;
this.Panel2.Visible = true;
string time,times;
GetVisitInfo(out time,out times);
this.lblDate.Text = time.ToString();
this.lblTimes.Text = times.ToString();
string city = this.GetCityCookie();
if(city=="")
{
this.hlCity.NavigateUrl = "EnterCity.aspx";
}
else
{
this.hlCity.NavigateUrl = "";
this.hlCity.Text = "From:" + city;
this.hlCity.ForeColor= Color.Black;
}
}
//sum
private int Sum(string str)
{
uint sum=0;//,i=0;
foreach(char a in str)
{
// if(i%2==1)//even order number
// {
//
// }
// i++;
sum += a;
if(sum>65535)//control the max value
{
sum &= 0x0ffff;
}
}
return (int)sum;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -