在c#中使用会话

时间:2015-07-03 14:55:15

标签: c# sql asp.net

这是我在C#中使用会话进行登录的代码。我为此编写了业务逻辑和数据访问层,但我的代码没有按预期工作。即使数据库中没有记录,我也可以登录并重定向到'<option>'...'</option>'

Default.aspx.cs

error.aspx

}

BL_Authenticate

public void LoginButton_Click(object sender, System.EventArgs e)
{
    int id;

    if (LoginName.Text!=""&& Password.Text!="")
    {
        try
        {
            sessionVars = BL_Authenticate.AuthenticateUser(sessionVars, LoginName.Text, Password.Text);

            Response.Redirect("home.aspx");
        }
        catch (Exception ex)
        {
            Session["Exception"] = ex.Message.ToString();
            Response.Redirect("error.aspx");
        }

        //else
        //{
        //    Response.Redirect("error.aspx");

        //}
        if (sessionVars.Tables[0].Rows.Count >= 1)
        {
            try
            {
                Session["User"] = (string)sessionVars.Tables[0].Rows[0]["FirstName"];
                Session["User"] += (string)" ";
                Session["User"] += (string)sessionVars.Tables[0].Rows[0]["LastName"];
            }
            catch (Exception ex)
            {
                Session["Exception"] = ex.Message.ToString();
                Response.Redirect("error.aspx");
            }
            id = (int)sessionVars.Tables[0].Rows[0][0];
            if (id >= 1)
            {
                try
                {
                    Session["Role"] = "Admin";
                    FormsAuthentication.Authenticate((string)sessionVars.Tables[0].Rows[0]["Login"], (string)sessionVars.Tables[0].Rows[0]["Password"]);
                }
                catch (Exception ex)
                {
                    Session["Exception"] = ex.Message.ToString();
                    Response.Redirect("error.aspx");
                }
                if (FormsAuthentication.GetRedirectUrl("Admin", false) == "/UserInterface/home.aspx")
                {
                    FormsAuthentication.RedirectFromLoginPage("admin", false);
                    Response.Redirect("home.aspx");
                }
                else
                    FormsAuthentication.RedirectFromLoginPage("admin", false);
            }
            else
            {
                Session["Role"] = "User";
                FormsAuthentication.RedirectFromLoginPage("user", false);
            }
        }
        else
        {
            errorMessage.Text = "Sorry, wrong username or password.";
        }
    }

}

DAL_Authenticate

public class BL_Authenticate
{
    public static DataSet AuthenticateUser(DataSet user, string login, string password)

    {
        return DAL_Authenticate.AuthenticateUser(user, login, password);
    }
}

1 个答案:

答案 0 :(得分:0)

我看到的一件正常事情是,如果登录失败,它会重定向到错误页面,所以那里没有错误,你确定你的登录工作是在那时工作吗?