📄 login.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 BookManage.BLL;
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public partial class Migrated_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
//如果是从别的页面重定向过来则显示提示信息
if (Request.QueryString["ReturnUrl"] != null)
{
lblNotify.Text = "您请求的页面需要登录";
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
protected void btLogin_Click(object sender, System.EventArgs e)
{
EMUser user = new EMUser();
if (Page.IsValid)
{
//检查用户登录并返回用户权限
int userPower = user.Login(txtUserName.Text, txtPwd.Text);
//如果用户权限为-1则未通过验证
if (userPower == -1)
{
lblNotify.Text = "用户名或密码错误";
return;
}
else
{
//如果通过验证则创建Forms 身份验证票据
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
txtUserName.Text,
DateTime.Now,
DateTime.Now.AddMonths(1),
cbAutoLogin.Checked,
userPower.ToString());
//加密票据
string enTicket = FormsAuthentication.Encrypt(ticket);
//将票据放入cookie中
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enTicket);
if (cbAutoLogin.Checked)
{
//设置cookie过期时间
cookie.Expires = DateTime.Now.AddMonths(1);
}
//添加cookie
Response.Cookies.Add(cookie);
//重定向到用户之前请求的url
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Text, cbAutoLogin.Checked));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -