温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Asp.net2.0的电影在线点播系统片段源码
当前文件:
MovieShowSample/Default.aspx.cs,打开代码结构图
MovieShowSample/Default.aspx.cs,打开代码结构图1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.WebControls; 8
using System.Web.UI.WebControls.WebParts; 9
using System.Web.UI.HtmlControls; 10
11
public partial class _Default : System.Web.UI.Page 12
{ 13
14
protected void Page_Load(object sender, EventArgs e) 15
{ 16
//从cookie中获取身份验证票信息 17
string cookieName = FormsAuthentication.FormsCookieName; 18
HttpCookie authCookie=Context.Request.Cookies[cookieName]; 19
20
21
if (authCookie != null) 22
{ 23
div2.Visible = true; 24
//解密 25
FormsAuthenticationTicket authTicket=FormsAuthentication.Decrypt(authCookie.Value); 26
Link.Text = authTicket.Name.ToString(); 27
LinkRole.Text = authTicket.UserData.ToString(); 28
29
} 30
else 31
{ div2.Visible = false; } 32
} 33
34
protected void login_Click(object sender, ImageClickEventArgs e) 35
{ 36
if (txtid.Text != "" && txtpwd.Text != "") 37
{ 38
string UserName =txtid.Text.Trim(); 39
string UserPwd = txtpwd.Text.Trim(); 40
41
DataVisit mydv = new DataVisit();//创建访问层的类 42
if (mydv.Confirm(UserName, UserPwd))//验证用户是否合法 43
{ 44
string UserRoles = mydv.GetRoles(UserName);//获取用户角色的字符串 45
//建立身份验证票据对象 46
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddHours(1), false, UserRoles, "/"); 47
//加密序列化验证票为字符串 48
string HashTicket = FormsAuthentication.Encrypt(Ticket); 49
//创建cookie 50
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName,HashTicket); 51
//输出cookie 52
Context.Response.Cookies.Add(UserCookie); 53
// div2.Visible = true; 54
Context.Response.Redirect( "Default.aspx"); 55
56
57
58
59
} 60
else 61
{ 62
txtid.Text = "不正确!用户名或密码"; 63
64
} 65
66
} 67
} 68
69
protected void Linkout_Click(object sender, EventArgs e) 70
{ 71
System.Web.Security.FormsAuthentication.SignOut(); 72
Response.Redirect("Default.aspx"); 73
} 74
75
76
77
78
protected void register_Click(object sender, ImageClickEventArgs e) 79
{ 80
Response.Redirect("Register.aspx"); 81
} 82
83
}





}
}