登陆界面
Login.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
namespace RegeditOfAdoDotnet
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//清除缓存
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "No-Cache");
}
/// <summary>
/// 登陆
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnLogin_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
if (Membership.ValidateUser(txtUserName.Text, txtPassWord.Text))//验证提供的用户和密码是否有效的
{
if (Roles.IsUserInRole(txtUserName.Text, "Admin"))//判断当前登陆的用户是不是管理员
{
// 自己写的验证票--创建身份验证票
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(20), false, "Admin");
// 创建身份验证票的加密字符串
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// 将加密字符串存储在HttpCookie 对象中
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
// 将此cookie 添加到返回给用户浏览器的cookie 集合中
Response.Cookies.Add(authCookie);
// 将用户重定向到最初请求的页
Response.Redirect("~/Admin/AdminInfo.aspx", true);
}
else
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);//将经过身份验证的用户重定向回最初请求的URL 或默认URL。
FormsAuthentication.SetAuthCookie(txtUserName.Text, false);//为提供的用户名创建一个身份验证票证,并将其添加到响应的Cookie 集合或URL。
Response.Redirect("~/Admin/GeneralUserInfo.aspx", true);
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "myscript", "<script>alert('登陆失败,请检查用户名和密码,重新输入!');</script>");
}
}
catch (Exception exp)
{
//捕获异常
Response.Write(exp.Message); //输出错误信息
}
}
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExit_Click(object sender, EventArgs e)
{
Session.Clear();
FormsAuthentication.SignOut();
Response.Redirect("~/Goodby.aspx");
}
}
}