首次尝试登录asp.net应用程序失败

时间:2019-11-28 14:52:58

标签: authentication asp.net-membership httpcookie formsauthenticationticket

Am登录页面(login.aspx)出现问题。第一次尝试失败,但第二次成功。为了确保从数据库中提取数据,我在ValidateUser()函数之后放置了一个断点。在第一次和第二次尝试中,函数均返回true。但是,第一次尝试不会将用户重定向到默认页面。 有人可以帮我确定我哪里出错了! 下面是我的代码

1.Page_Load事件

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Title = AppName + this.Title;
                if (Session["userinfo"] != null)
                {
                    Response.Redirect("~/SecuredPages/Home.aspx");
                }
            }
        }
  1. 点击事件
protected void btnLogin_Click(object sender, EventArgs e)
        {
            lblError.Visible = false;
            bool Valid = false;
            Valid = Membership.ValidateUser(txtUName.Text, txtPassword.Text);
            if (Valid)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(txtUName.Text, true, sessionTimeOut);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                if (ticket.IsPersistent) { authCookie.Expires = ticket.Expiration; }
                Response.Cookies.Add(authCookie);
                Session["userinfo"] = Membership.GetUser(User.Identity.Name);
                Session["UserName"] = User.Identity.Name;
                Response.Redirect("~/SecuredPages/Home.aspx");
            }
            else
            {
                lblError.Text = "Invalid user name and/or password";
                lblError.Visible = true;
            }
        }

0 个答案:

没有答案
相关问题