http://localhost:49805/WebSite1/Default.aspx.cs

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");//跳转
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值