using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
private int login(string username, string userpass)
{
Dao.sds.SelectCommand = "select count(*) from t_user where username = ? and userpass = ?";
Dao.sds.SelectParameters.Clear();
Dao.sds.SelectParameters.Add("1", username);
Dao.sds.SelectParameters.Add("2", userpass);
IEnumerable ie = Dao.sds.Select(DataSourceSelectArguments.Empty);
DataView dv = (DataView)ie;
int i = 0;
int.TryParse(dv[0][0].ToString(), out i);
return i;
}
protected void Page_Load(object sender, EventArgs e)
{
this.Login1.Authenticate += new AuthenticateEventHandler(Login1_Authenticate);//委托
if (Request.Cookies["username"] == null || Request.Cookies["userpass"] == null)//如果cookies为空就停止
{
return;
}
string username = Request.Cookies["username"].Value;//获取到cookies
string userpass = Request.Cookies["userpass"].Value;
if (this.login(username, userpass) == 0)//到数据库验证一下,等于0就是没有找到,那就停止
{
return;
}
Response.Redirect("~/Index.aspx");
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = this.Login1.UserName;//获取控件的值
string userpass = this.Login1.Password;
if (this.login(username, userpass) == 0)//到数据库验证一下,等于0就是没有找到,那就停止
{
return;
}
Response.Cookies["username"].Value = username;//设置到cookies
Response.Cookies["userpass"].Value = userpass;
if (this.Login1.RememberMeSet)//如果选择了记住我就...
{
Response.Cookies["username"].Expires = DateTime.Now.AddDays(7);//就设置cookies时间为7天
Response.Cookies["userpass"].Expires = DateTime.Now.AddDays(7);
}
Response.Redirect("~/Index.aspx");//跳转
}
}
http://localhost:49805/WebSite1/Default.aspx.cs
最新推荐文章于 2020-07-22 10:12:48 发布