繁体   English   中英

n层架构中的ASP.NET登录

[英]ASP.NET Login in n-tier architecture

我正在尝试在基于n层体系结构的ASP.NET C#中实现登录功能。

资料存取:

public int userlogin(string user, string passw)//checking the user name and password
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = GetConnectionString();
    con.Open();
    int id = 0;
    string selectstr = "SELECT NurseName, password FROM Nurse2 WHERE NurseName = '" + user.Trim() + "' AND Password = '" + passw.Trim() + "'";
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = selectstr;
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.Connection = con;
    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        id++;
    }
    cmd = null;
    reader.Close();
    con.Close();
    return id;
}

表示层.cs文件

protected void Button1_Click(object sender, EventArgs e)
{
    string name = TextBox1.Text;
      string password = TextBox2.Text;
    int id = da.userlogin(name, password);
    if (id > 0)
    {
        Session["userName"] = name;

        Response.Redirect("SubscribePage.aspx");

    }
    else
    {
        Label1.Text = "invalid";
    }

现在,我的问题是,即使我输入正确的数据,当我按下按钮时,程序也会简单地转到else子句。 在我看来,这里可能不行的事情似乎还不错。

我认为您现在不需要这样做。 ASP.NET具有内置身份验证。 只需检查一下https://msdn.microsoft.com/en-us/library/xdt4thhy(v=vs.140).aspx

N-Tier体系结构有助于分离您的代码,因为您的代码正在跳过一层并且没有充分利用业务逻辑层。 这是一个有用的图像;

图表示

我还将添加一个额外的类来存储您的用户登录详细信息,我想您将拥有更多信息以及要存储的护士姓名-您可以将此类的实例存储在会话数据中,并在需要时将其丢弃;

public class User
{
    public string Name        { get; set; }
    /* Some other attributes - not your password though! */
}

-

介绍;

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Session["User"] = BLL.userLogin(TextBox1.Text, TextBox2.Text);
            Response.Redirect("SubscribePage.aspx"); /* If it reaches here, everything is okay */
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }

业务层;

    public static User userLogin(string username, string password)
    {
        User U = DAL.userLogin(username, password);

        if (string.IsNullOrEmpty(U.Name))
            throw new Exception("Incorrect login details");

        return U;
    }

数据访问层;

    public static User userLogin(string username, string password)
    {
        using (SqlConnection con = new SqlConnection(GetConnectionString())
        {
            User U = new User();

            SqlCommand cmd = new SqlCommand(@"SELECT NurseName, password 
                                                FROM Nurse2 
                                                WHERE NurseName = @user AND password = @pw", con);

            cmd.Parameters.Add(new SqlParameter("@user", username));
            cmd.Parameters.Add(new SqlParameter("@pw", password));

            try
            {
                con.Open();
            }
            catch (Exception ex)
            {
                throw new Exception("connetion problem", ex);
            }

            try
            {
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        U = rdr["NurseName"];
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("problem with query", ex);
            }
            finally
            {
                con.Close(); /* Clean up regardless of the outcome */
                con.Dispose();
            }

            return U;
        }
    }

阅读有关N-Tier体系结构的更多信息,并尝试try-catch语句。 希望能帮助到你。 我还将改进控件的命名约定,以使生活更轻松(即Label1-> lblError)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM