Use a three-tier architecture to write user logon (ASP. NET)

Source: Internet
Author: User

First, set up the shelf, as shown in figure

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1644595061-0.jpg "/>

Next, write the data at a layer. First, write the data at the model layer. For example:

Public class User
{
Private string userId;

Public string UserId
{
Get {return userId ;}
Set {userId = value ;}
}
Private string userPwd;

Public string UserPwd
{
Get {return userPwd ;}
Set {userPwd = value ;}
}
Private string userName;

Public string UserName
{
Get {return userName ;}
Set {userName = value ;}
}
Private string userSex;

Public string UserSex
{
Get {return userSex ;}
Set {userSex = value ;}
}
Private string userTel;

Public string UserTel
{
Get {return userTel ;}
Set {userTel = value ;}
}
Private string userAddress;

Public string UserAddress
{
Get {return userAddress ;}
Set {userAddress = value ;}
}
Private string userEmail;

Public string UserEmail
{
Get {return userEmail ;}
Set {userEmail = value ;}
}
Private string userImg;

Public string UserImg
{
Get {return userImg ;}
Set {userImg = value ;}
}
}

Then there is the data link layer, for example:

Public class User_DAL
{Public object CheckLogin (User user, string right)
{
String sqlText;
If (right = "Administrator ")
{
SqlText = "select userPwd from user_tb where userId = @ userId ";
}
Else if (right = "department manager ")
{
SqlText = "select empPwd from Employee_tb where empId = @ userId and empType = 'department manager '";
}
Else if (right = "CR employee ")
{
SqlText = "select empPwd from Employee_tb where empId = @ userId and empType = 'cr employees '";
}
Else if (right = "driver ")
{
SqlText = "select empPwd from Employee_tb where empId = @ userId and empType = 'drivers '";
}
Else if (right = "Operation employee ")
{
SqlText = "select empPwd from Employee_tb where empId = @ userId and empType = 'employee operation '";
}
Else
{
SqlText = "select cusPwd from Customer_tb where cusId = @ userId ";
}
SqlParameter [] paras = new SqlParameter [1]
{
New SqlParameter ("userId", user. UserId)
};
Object obj = SqlLink. GetScaler (sqlText, paras );
Return obj;
}

}

Then there is the business logic layer; for example:

Public class User_BLL
{
User_DAL userdal = new User_DAL ();
Public int CheckLogin (User user, string right)
{
Object obj = userdal. CheckLogin (user, right );
Int result =-1;
If (obj = null)
{
Result = 0; // the user does not exist.
}
Else if (obj. ToString (). Trim () = user. UserPwd)
{
Result = 1; // correct
}
Else if (obj. ToString (). Trim ()! = User. UserPwd)
{
Result = 2; // incorrect password
}
Else
{
Result = 3; // System Error
}
Return result;
}
}

The presentation layer. For example:

 

Protected void BtnLogin_Click (object sender, EventArgs e)
{
Try
{
// Verify Logon
If (txtUserId. Text = "" | txtUserPwd. Text = "")
{
Response. Write ("<script> alert ('user name or password cannot be blank ') </script> ");
Return;
}
String userId = txtUserId. Text. Trim ();
String userPwd = txtUserPwd. Text. Trim ();
String right = cobRight. Text. Trim ();
User. UserId = userId. Trim ();
User. UserPwd = userPwd. Trim ();
Int I = userbll. CheckLogin (user, right );
If (I = 0)
{
Response. Write ("<script> alert ('user name does not exist') </script> ");
}
Else if (I = 1)
{
Session ["userId"] = txtUserId. Text;
Session ["userPwd"] = txtUserPwd. Text;
Server. Transfer ("Index. aspx ");
}
Else if (I = 2)
{
Response. Write ("<script> alert ('wrong password') </script> ");
}
Else if (I = 3)
{
Response. Write ("<script> alert ('System error') </script> ");
}

}
Catch (Exception ex)
{
Response. Write (ex. Message );
}

The presentation layer only writes the method, because you need to change the name as needed. If necessary, please leave a message.

This article from the "Love-JS" blog, please be sure to keep this source http://leileiaijishu.blog.51cto.com/6286126/1090948

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.