using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ManagementSystem.Utility;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Security.Cryptography;
namespace ManagementSystem
{
public partial class LoginForm : Form
{
SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID);
SqlCommand sqlCmd = new SqlCommand();
SqlDataReader sdrData = null;
public LoginForm()
{
InitializeComponent();
}
#region 自定義程式
public static DataSet GetAccountingBookList() //取得帳本資料
{
DataSet dsData = new DataSet();
try
{
string strCommand = string.Format("Select AccountingBookID,AccountingBookName From OTB_FNC_AccountingBookInfo Where Effective='Y'");
using (SqlConnection sqlConn = UtilityClass.GetConn(MainForm.strAccountingBookID))
{
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strCommand, sqlConn))
{
if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
{
sqlConn.Open();
}
sqlAdapter.Fill(dsData, "AccBookList");
//增加空白選項
DataRow drData = dsData.Tables["AccBookList"].NewRow();
drData["AccountingBookID"] = " ";
drData["AccountingBookName"] = "請選擇";
dsData.Tables["AccBookList"].Rows.Add(drData);
//資料進行排序
dsData.Tables["AccBookList"].DefaultView.Sort = "AccountingBookID ASC";
}
}
return dsData;
}
catch (Exception ex)
{
throw ex;
}
}
public bool DataCheck() //確認資料正確性
{
if (txtID.Text.Trim() == "")
{
MessageBox.Show("請輸入帳號", "提示");
txtID.Focus();
return false;
}
if (cbAccountingBook.SelectedValue.ToString().Trim() == "")
{
MessageBox.Show("請選擇帳本", "提示");
cbAccountingBook.Focus();
return false;
}
return true;
}
private void Login()
{
if (DataCheck())
{
sqlCmd.Connection = sqlConn;
string strCmd = string.Format("Select MemberID From OTB_SYS_Members Where MemberID = '{0}' and Password='{1}'", txtID.Text.Trim(), Encrypt(txtPassword.Text.Trim()).ToString());
sqlCmd.CommandText = strCmd;
try
{
if (sqlConn.State == ConnectionState.Open)
{
sdrData = sqlCmd.ExecuteReader();
}
else
{
sqlConn.Open();
sdrData = sqlCmd.ExecuteReader();
}
if (sdrData.HasRows)
{
MainForm.strKey = txtKey.Text.Trim(); //記錄Key值
MainForm.strActiveUserID = txtID.Text.Trim(); //記錄作業人員
MainForm.strAccountingBookID = cbAccountingBook.SelectedValue.ToString(); //記錄作業帳本
switch (cbAccountingBook.SelectedValue.ToString()) //顯示選擇帳本
{
case "Origtek":
((MainForm)Owner).SsStatus.Items["tsslAccountingBook"].Text = "元赫科技";
((MainForm)Owner).SsStatus.Items["tsslAccountingBook"].ForeColor = System.Drawing.Color.FromName("Blue");
break;
case "OrigtekEnergy":
((MainForm)Owner).SsStatus.Items["tsslAccountingBook"].Text = "元赫環科";
((MainForm)Owner).SsStatus.Items["tsslAccountingBook"].ForeColor = System.Drawing.Color.FromName("Red");
break;
}
this.Close();
}
else
{
MessageBox.Show("登入失敗,您的帳號、密碼可能有誤!", "注意");
}
}
catch (Exception ex)
{ }
finally
{
sdrData.Close();
sdrData = null;
}
}
}
#endregion
#region 事件觸發及問題處理
private void LoginForm_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterParent;
cbAccountingBook.DataSource = GetAccountingBookList().Tables["AccBookList"];
cbAccountingBook.ValueMember = "AccountingBookID";
cbAccountingBook.DisplayMember = "AccountingBookName";
cbAccountingBook.SelectedIndex = 1; //方便測試使用
}
private void btClear_Click(object sender, EventArgs e)
{
txtID.Text = "";
txtPassword.Text = "";
cbAccountingBook.SelectedIndex = 0;
txtKey.Text = "";
}
private void btClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btLogin_Click(object sender, EventArgs e)
{
Login();
}
private void txtPassword_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Login();
}
}
#endregion
#region 登入時密碼驗證程式
///
/// 帳號加密程式
///
///
///
private static string Encrypt(string str)
{
string sEncrypt = DESEncrypt(str, ConfigurationManager.AppSettings["DefaultCryptionKey"].Trim(), ConfigurationManager.AppSettings["DefaultCryptionIV"].Trim());
return sEncrypt;
}
//加密程式
private static string DESEncrypt(string data, string strKey, string strIV)
{
//將key轉成utf8編碼 byte array
byte[] tmpkey = System.Text.Encoding.UTF8.GetBytes(strKey);
//將iv轉成utf8編碼 byte ayyay
byte[] tmpIV = System.Text.Encoding.UTF8.GetBytes(strIV);
MD5CryptoServiceProvider mD5Provider = new MD5CryptoServiceProvider();
byte[] key = mD5Provider.ComputeHash(tmpkey);
byte[] iv = mD5Provider.ComputeHash(tmpIV);
//將data轉成utf8編碼 byte ayyay
byte[] byteData = Encoding.UTF8.GetBytes(data);
//加密
RijndaelManaged aesProvider = new RijndaelManaged();
ICryptoTransform aesEncrypt = aesProvider.CreateEncryptor(key, iv);
byte[] result = aesEncrypt.TransformFinalBlock(byteData, 0, byteData.Length);
//轉成base64字串
return Convert.ToBase64String(result);
}
#endregion
}
}