Esempio n. 1
0
        private void BtUserLogin_Click(object sender, RoutedEventArgs e)
        {
            #region 取来账号和密码的信息
            string userID       = TextUserID.Text.Trim().ToString();
            IntPtr p            = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.TextUserPassword.SecurePassword);
            string userPassword = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            #endregion

            #region 连接数据库 完成登录验证
            SqlConnection sqlConnection = new SqlConnection(ConfigManager.ConnectStr);//数据库对象

            try
            {
                sqlConnection.Open();
                string         sql        = "select * from userInfo where userID=@userID and userPassword=@userPassword ";
                SqlParameter[] parameters = new SqlParameter[2]
                {
                    new SqlParameter("@userID", userID),
                    new SqlParameter("@userPassword", userPassword)
                };
                DataTable dataSet = SqlHelper.ExcuteTable(sql, CommandType.Text, parameters);
                sqlConnection.Close();//关闭数据库

                #region 对返回的数据进行判断用户是否成功登录
                if (dataSet.Rows.Count == 0)
                {
                    MessageBox.Show("登录失败,请检查账号密码是否正确");
                }
                else
                {
                    #region 完成用户信息的读取
                    UserInfo.UserID         = dataSet.Rows[0][0].ToString();
                    UserInfo.UserName       = dataSet.Rows[0][2].ToString();
                    UserInfo.UserPhone      = dataSet.Rows[0][3].ToString();
                    UserInfo.UserMail       = dataSet.Rows[0][4].ToString();
                    UserInfo.UserProfession = dataSet.Rows[0][5].ToString();
                    #endregion

                    #region 跳转用户用户界面,关闭登录窗口
                    UserWindow userWindow = new UserWindow();
                    userWindow.Show();
                    this.Close();//关闭当前窗口
                    #endregion

                    //写入日志
                    string log = string.Format("用户:{0},登录该系统成功,登录ip:{1}\n", userID, IPAddress.Loopback);
                    WriteLog(log);
                }
                #endregion
            }
            catch
            {
                if (sqlConnection.State == ConnectionState.Open)
                {
                    sqlConnection.Close();
                }
                WriteLog("登录系统异常\n");
            }
            #endregion
        }
        // Handle server response to Login request.
        public async void ServerResponse(User currentUser)
        {
            byte[] answer = new byte[1];

            // Read answer from server.
            await stream.ReadAsync(answer, 0, 1);

            switch (answer[0])
            {
            case (int)EServerResponse.UserNotExist:

                ShowErrorLabel(userNotExist);
                break;

            case (int)EServerResponse.SuccessSignIn:

                if (uploadFiles == null)
                {
                    GetAllFiles(currentUser.UploadPath.Trim());
                }

                UserWindow userControlPanel = new UserWindow(stream, uploadFiles, currentUser);
                userControlPanel.Show();

                errorLabel.Visibility = Visibility.Hidden;
                this.Close();
                break;

            case (int)EServerResponse.UserAlredyConnected:

                ShowErrorLabel(userAlreadySignIn);
                break;

            case (int)EServerResponse.UserDisable:

                ShowErrorLabel(userDisable);
                break;
            }
        }