📄 formauthentication.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 System.Web.Security;
using System.Security.Principal;
using System.Data.SqlClient;
namespace Example_14_5
{
/// <summary>
/// FormAuthentication 的摘要说明。
/// </summary>
public class FormAuthentication : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox UserName;
protected System.Web.UI.WebControls.Button LoginBtn;
protected System.Web.UI.WebControls.Label LoginMsg;
protected System.Web.UI.WebControls.TextBox Password;
private void Page_Load(object sender, System.EventArgs e)
{
//
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.LoginBtn.Click += new System.EventHandler(this.LoginBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LoginBtn_Click(object sender, System.EventArgs e)
{
///判断用户的输入
if(UserName.Text.Trim().Length <= 0 || Password.Text.Trim().Length <= 0)
{
LoginMsg.Text = "用户名称或密码为空,请重新输入!!!";
return;
}
//验证初始化
FormsAuthentication.Initialize();
UserDB user = new UserDB();
SqlDataReader recu = user.GetUserLogin(UserName.Text.Trim(),Password.Text.Trim());
if(recu.Read())
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
UserName.Text.Trim(),
DateTime.Now,
DateTime.Now.AddMinutes(20),
true,
recu["UserID"].ToString(),
FormsAuthentication.FormsCookiePath);
string ticketHash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
ticketHash);
if(ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
Response.Cookies.Add(cookie);
Response.Redirect("AdminPage.aspx");
}
else
{
LoginMsg.Text = "用户名称或密码输入错误,请重新输入!!!";
}
recu.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -