UserLogin
DAL:
IUserDAL
namespace Dal { /// <summary> /// This interface is defined for user functions. /// </summary> public interface IUserDal { #region Returns Model /// <summary> /// Gets user by username. /// </summary> /// <param name="username">The user's name.</param> /// <returns>Returns user model.</returns> User RetrieveUserByUserName(string userName); #endregion #region Public int /// <summary> /// Updates the password of the user. /// </summary> /// <param name="newPassword">The new password.</param> /// <param name="userName">The user's name.</param> int UpdatePassword(string newPassword, string userName); #endregion } }
UserDAL
namespace Dal { /// <summary> /// This class is used for defining user functions. /// </summary> public class UserDal : IUserDal { #region Returns Model /// <summary> /// Gets user by username. /// </summary> /// <param name="userName">The user's name</param> /// <returns>Return user model.</returns> public User RetrieveUserByUserName(string userName) { User user = null; string sqlText = SqlText.GetUserByUserName; SqlParameter[] prams = new SqlParameter[] { new SqlParameter("@username",userName), }; try { SqlDataReader sqlDataReader = SqlHelper.ExecureReader(sqlText, prams); if (sqlDataReader.Read()) { user = new User(); user.UserName = userName; user.Password = Convert.ToString(sqlDataReader["password"]); user.RoleType = Convert.ToString(sqlDataReader["role_type"]); user.Telephone = Convert.ToString(sqlDataReader["telephone"]); user.Gender = sqlDataReader["gender"].ToString(); user.Email = sqlDataReader["email"].ToString(); user.Address = sqlDataReader["address"].ToString(); user.ChineseName = (sqlDataReader["chinese_name"] is DBNull) ? string.Empty : sqlDataReader["chinese_name"].ToString(); user.Language = (sqlDataReader["language"] is DBNull) ? string.Empty : sqlDataReader["language"].ToString(); } SqlHelper.CloseSqlDataReader(sqlDataReader); } catch(SqlException ex) { throw new UserException(UserException.RetrieveUserByUserName,ex); } return user; } #endregion #region Public int /// <summary> /// Updates the password of the user. /// </summary> /// <param name="newPassword">The new password.</param> /// <param name="userName">The user's name.</param> public int UpdatePassword(string newPassword, string userName) { int influenceNumber = 0; try { string sqlText = SqlText.UpdatePassword; SqlParameter[] parms = new SqlParameter[] { new SqlParameter("@password", newPassword), new SqlParameter("@userName", userName), }; influenceNumber = SqlHelper.ExecuteNonQuery(sqlText, parms); } catch (SqlException ex) { throw new UserException(UserException.UpdatePasswordFailed, ex); } return influenceNumber; } #endregion } }
BLL
IUserBLL
namespace Bll { /// <summary> /// This interface is used to define user's functions. /// </summary> [ServiceContract(Namespace="MyCompanyService")] public interface IUserBll { #region Returns Model /// <summary> /// Gets the user model. /// </summary> /// <param name="userName">The user's name.</param> /// <returns>Returns user's model.</returns> [OperationContract] User RetrieveUserByUserName(string userName); #endregion #region Returns bool /// <summary> /// Updates user's password. /// </summary> /// <param name="newPassword">The new password.</param> /// <param name="userName">The user's name.</param> [OperationContract] bool UpdatePassword(string newPassword, string userName); #endregion } }
UserBLL
namespace Bll { /// <summary> /// This class is used for realizing user's functions. /// </summary> public class UserBll:IUserBll { #region Field IUserDal userDal = new UserDal(); log4net.ILog log = log4net.LogManager.GetLogger(EqualsConst.GetServiceLoggerName); #endregion public UserBll() { } public UserBll(IUserDal userDal) { this.userDal = userDal; } #region Returns Model /// <summary> /// Gets the user by user's name. /// </summary> /// <param name="userName">The user's name.</param> /// <returns>Returns user model.</returns> public User RetrieveUserByUserName(string userName) { User user = null; try { user = userDal.RetrieveUserByUserName(userName); } catch (UserException ex) { log.Error(ExamException.RetrieveExamList, ex); throw new FaultException<MyExceptionContainer>(new MyExceptionContainer() { ErrorMessage = ex.Message, Description = ExamException.RetrieveExamList }); } return user; } #endregion #region Public bool /// <summary> /// Updates user's password. /// </summary> /// <param name="newPassword">The new password.</param> /// <param name="userName">The user's name.</param> public bool UpdatePassword(string newPassword, string userName) { bool isUpdatePassworded = false; try { int i = userDal.UpdatePassword(newPassword, userName); if (i > 0) { isUpdatePassworded = true; } } catch (UserException ex) { log.Error(UserException.UpdatePasswordFailed, ex); throw new FaultException<MyExceptionContainer>(new MyExceptionContainer() { ErrorMessage = ex.Message, Description = UserException.UpdatePasswordFailed }); } return isUpdatePassworded; } #endregion } }
Client
namespace OES { /// <summary> /// This class is used for login. /// </summary> public partial class FormLogin : Form { #region Field private log4net.ILog log = log4net.LogManager.GetLogger(EqualsConst.GetLoggerName); public User user = null; #endregion #region Constructor public FormLogin() { InitializeComponent(); } #endregion #region Private Method /// <summary> /// The processing of btnSubmit. /// </summary> /// <param name="sender">The source object of event.</param> /// <param name="e">The parameter of event.</param> private void BtnSubmit_Click(object sender, EventArgs e) { UserBllService.UserBllClient userBll = new UserBllService.UserBllClient(); if (String.IsNullOrWhiteSpace(this.txtUsername.Text)) { this.lblLoginResult.Text = Constant.UserException.UserNameIsNull; } else if (String.IsNullOrWhiteSpace(this.txtPassword.Text)) { this.lblLoginResult.Text = Constant.UserException.PasswordIsNull; } else { string userName = CheckForParameter.ReplaceSqlChar(this.txtUsername.Text.Trim()); string password = CheckForParameter.ReplaceSqlChar(this.txtPassword.Text.Trim()); try { BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler( delegate { user = userBll.RetrieveUserByUserName(userName); }); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler( delegate(object obj, RunWorkerCompletedEventArgs arg) { if (arg.Error == null) { if (null != user && user.Password.Equals(MD5Tool.MD5Tostring(password), StringComparison.InvariantCultureIgnoreCase)) { this.DialogResult = DialogResult.OK; } else { this.lblLoginResult.Text = UserException.LoginedFailed; } } else { log.Error(arg.Error); MessageBox.Show(UIException.ConnectionWithWCFFail); } }); bw.RunWorkerAsync(); } catch (FaultException<MyExceptionContainer> myException) { log.Error(myException.Message, myException); } catch (FaultException faultException) { log.Error(faultException.Message, faultException); } catch (Exception exception) { log.Error(exception.Message, exception); } } } /// <summary> /// The processing of close the form. /// </summary> /// <param name="sender">The source object of event.</param> /// <param name="e">The parameter of event.</param> private void BtnCancel_Click(object sender, EventArgs e) { this.Close(); this.Dispose(); } #endregion } }