標籤:collect encrypt direct rect rip ble rgs 個人資訊 輸入
這是我的第一篇博文,有一絲小激動,不曾想有一天我也能寫出一點經驗為大家服務。如有表達不清請多見諒。
首先,我之前必須完成過註冊,並把個人資訊存入資料庫中。
其次,這部分的個別對象是存於某些文檔中的,需要引用命名空間。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZG.Common;//後面用到ScriptHelper對象(ScriptHelper.cs是自己編寫的cs檔案)
using System.Data;//後面用到dataset
namespace WebApplication
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 登入按鈕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnLogin_Click(object sender, EventArgs e)
{
//使用者表 Sys_User 列PersonStatus 為 “正常” 才可登入 不然提示賬戶狀態為PersonStatus內的內容
//列PersonCode為使用者名稱 PassWord為密碼
//資料庫中PassWord儲存的為加密後的 字串.Ext_DecryptString();為解密 Ext_EncryptString();為加密
string userName = txtUserName.Text.Trim();//.Trim()是去掉字串前後的Null 字元
string passWord = txtPwd.Text.Trim();
//.Ext_IsNullOrEmpty()是在另一個檔案中自己編寫的函數,用於判斷字串是否為空白字元(也可用userName==“”等判斷)
if (userName.Ext_IsNullOrEmpty())
{
ScriptHelper.ShowAlertScript("請輸入使用者名稱!");//彈出表單提示
return;
}
if (passWord.Ext_IsNullOrEmpty())
{
ScriptHelper.ShowAlertScript("請輸入密碼!");
return;
}
//在Sys_User 表中篩選出使用者名稱為userName的資料數量,如果為0表示沒有該使用者,為1表示有。
DataSet ds = SqlHelper.GetData("select count(*) from Sys_User where PersonCode=‘" + userName+ "‘");
if (ds.Tables[0].Rows[0][0].ToString() != "1")
{
ScriptHelper.ShowAlertScript("使用者名稱不存在!");
return;
}
//在Sys_User 表中篩選出使用者名稱為userName的PersonStatus 值。
DataSet dsStatus = SqlHelper.GetData("select PersonStatus from Sys_User where PersonCode=‘" + userName + "‘");
//取出dsStatus(小資料庫)中([0])第一張表的第一行中名為PersonStatus的列的值
string personStatus = dsStatus.Tables[0].Rows[0]["PersonStatus"].ToString();
if (personStatus != "正常")
{
ScriptHelper.ShowAlertScript("使用者狀態不正確:" + personStatus);
return;
}
//注意密碼的加密,Null 字元加密後便不是Null 字元了。資料庫中的密碼是加密後的字元,實際比較中需要用實際輸入字元經加密得到的字元與資料庫中的比較
//判斷密碼 法一 //string sql = "select * from Sys_User where PersonCode=‘{0}‘ and Password=‘{1}‘";
//DataSet dsUser = SqlHelper.GetData(string.Format(sql, userName, passWord.Ext_EncryptString()));
//if (dsUser.Tables[0].Rows.Count!=1)
//{
// ScriptHelper.ShowAlertScript("密碼不正確!");
// return;
//}//判斷密碼 法二 string sql = "select * from Sys_User where PersonCode=‘{0}‘ ";
DataSet dsUser = SqlHelper.GetData(string.Format(sql, userName));
if (dsUser.Tables[0].Rows[0]["PassWord"].ToString() != passWord.Ext_EncryptString())
{
ScriptHelper.ShowAlertScript("密碼不正確!");
return;
}
Session["UserName"] = dsUser.Tables[0].Rows[0]["PersonCode"].ToString();
Session["LoginUser"] = dsUser.Tables[0].Rows[0]["PersonName"].ToString();
Session["UserID"] = dsUser.Tables[0].Rows[0]["ItemID"].ToString();
//如果登入成功 跳轉到首頁
Response.Redirect("index.aspx");
}
}
}
利用DataSet部分功能實現網站登入