ASP.NET MVC 登陸驗證


1、 準備條件

DES界面類:用於解密連接字符串中DES加密連接字符串

MD5類:用於對用戶輸入的密碼進行MD5加密,與數據庫中的密碼(MD5加密後的密碼)進行對比。

SQLHelper類,操作數據庫用的

2、 Controllers

新建LoginController

增加 ActionResult Login(string username,string password)方法

public ActionResult Login(string userName, string password)

{

if (userName.Length <= 0 || password.Length <= 0)

{

//未輸入用戶名或密碼

}

//開始登陸驗證

if (ValidateUser(userName, password))

{

Session["username"] = userName;

//return View();

return Content("登陸成功" + userName);

}

else

{

return Content("登陸失敗");

}

}

/// <summary>

/// 登陸賬號密碼驗證

///

/// <param>

/// <param>

/// <returns>

public bool ValidateUser(string name, string pwd)

{

string sql = @"select * from Proj_Users Where Name=@UserName";

DataTable dt = SQLHelper.ExecuteDataTable(sql, new SqlParameter("@UserName", name));

if (dt.Rows.Count == 1) //只有一條記錄

{

//此時,賬號正確,進行密碼匹配

if (MD5.GetMd5(pwd) == dt.Rows[0]["Password"].ToString())

{

return true;

}

else

{

//密碼錯誤,

return false;

}

}

else

{

//沒有記錄或有多個記錄

//賬號不對,或系統錯誤,存在多個記錄

return false;

}

}

3、 View



分享到:


相關文章: