MVC+EasyUI+三層新聞網(wǎng)站建立 實現(xiàn)登錄功能(四)
MVC新聞網(wǎng)站建立,實現(xiàn)登錄功能
首先在數(shù)據(jù)庫中建立一張UserInfo表。

注:以下講的這些可以用動軟代碼生成器直接生成,但是對于新手來說還是動手敲一下的好,了解以下實現(xiàn)的過程。
然后在Model中建立UserInfo的實體層。
public class UserInfo
{
public int Id { get; set; }
public string UserName { get; set; }
public string UserPwd { get; set; }
public string UserMail { get; set; }
public DateTime RegTime { get; set; }
}
接著就在DAL層中建立UserInfo的數(shù)據(jù)庫訪問
//根據(jù)用戶名密碼查詢用戶
public UserInfo GetUserInfoModel(string userName, string userPwd)
{
string sql = "select * from T_UserInfo where UserName=@UserName and UserPwd=@UserPwd";
SqlParameter[] pms = {
new SqlParameter("@UserName",SqlDbType.NVarChar,32),
new SqlParameter("@UserPwd",SqlDbType.NVarChar,32)
};
//給參數(shù)賦值
pms[0].Value = userName;
pms[1].Value = userPwd;
DataTable dt = SqlHelper.ExcuteDataTable(sql, CommandType.Text, pms);
UserInfo userInfo = null;
if (dt.Rows.Count>0)
{
userInfo = new UserInfo();
LoadEntity(dt.Rows[0],userInfo);
}
return userInfo;
}
private void LoadEntity(DataRow dataRow, UserInfo userInfo)
{
userInfo.Id = Convert.ToInt32(dataRow["Id"]);
//判斷是否為空
userInfo.UserName = dataRow["UserName"] != DBNull.Value ? dataRow["UserName"].ToString() : string.Empty;
userInfo.UserPwd = dataRow["UserPwd"] != DBNull.Value ? dataRow["UserPwd"].ToString() : string.Empty;
userInfo.UserMail = dataRow["UserMail"] != DBNull.Value ? dataRow["UserMail"].ToString() : string.Empty;
userInfo.RegTime = Convert.ToDateTime(dataRow["RegTime"]);
}
在BLL層中建立UserInfo的邏輯處理層UserInfoServices
DAL.UserInfoDal userInfoDal = new DAL.UserInfoDal();
public UserInfo GetUserInfoModel(string userName, string userPwd)
{
return userInfoDal.GetUserInfoModel(userName, userPwd);
}
這些都準備完畢后就到登錄頁面提交表單就可以了(在提交表單之前需要判斷用戶名、密碼、驗證碼是否為空,下面我做了一個簡單的判斷)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>登錄</title>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Content/EasyUI/jquery.easyui.min.js"></script>
<script src="~/Content/EasyUI/easyui-lang-zh_CN.js"></script>
<link href="~/Content/EasyUI/themes/default/easyui.css" rel="external nofollow" rel="stylesheet" />
<link href="~/Content/EasyUI/themes/icon.css" rel="external nofollow" rel="stylesheet" />
<script type="text/javascript">
$(function () {
initWin(); //初始化登錄窗體
changeCheckCode(); //切換驗證碼
cheakLogin(); //驗證登錄
});
//驗證登錄
function cheakLogin() {
$("#btnOk").click(function () {
if ($("#txtName").val() == "") {
$("#spanName").text("必填");
}
else {
$("#spanName").text("");
}
if ($("#txtPwd").val() == "") {
$("#spanPwd").text("必填");
}
else {
$("#spanPwd").text("");
}
if ($("#txtVcode").val() == "") {
$("#spanVcode").text("必填");
}
else {
$("#spanVcode").text("");
}
if ($("#txtName").val() != "" && $("#txtPwd").val() != "" && $("#txtVcode").val() != "") {
//先把表單序列化為json對象
var jsonForm = $("#loginForm").serializeArray();
//把數(shù)據(jù)異步提交到后臺
$.ajax({
type: "post",
url: "/Login/CheckLogin",
data: jsonForm,
success: function (data) {
var serverData = data.split(':');
if (serverData[0]=="ok") {
window.location.href = "/Home/Index";
}
else if (serverData[0] == "no") {
$("#spanCheak").text(serverData[1]);
}
else {
$("#spanCheak").text("異常錯誤");
}
}
});
}
});
}
//初始化登錄窗體
function initWin() {
$("#win").window({
title: "登錄",
width: 400,
height: 270,
collapsible: false,
minimizable: false,
maximizable: false,
closable: false,
modal: true,
resizable: false,
});
}
//切換驗證碼
function changeCheckCode() {
$("#changeVcode").click(function () {
$("#image").attr("src", $("#image").attr("src") + 1);
});
}
</script>
</head>
<body>
<div id="win" class="easyui-window">
<div>
<div style="height:20px"></div>
<form id="loginForm">
<table>
<tr>
<td style="width:20px"></td>
<td>用戶名:</td>
<td><input type="text" class="easyui-textbox" id="txtName" name="txtName" /></td>
<td><span id="spanName" style="color:red"></span></td>
</tr>
<tr style="height:10px"></tr>
<tr>
<td style="width:20px"></td>
<td>密 碼:</td>
<td><input type="password" class="easyui-textbox" id="txtPwd" name="txtPwd"></td>
<td><span id="spanPwd" style="color:red"></span></td>
</tr>
<tr style="height:10px"></tr>
<tr>
<td style="width:20px"></td>
<td>驗證碼:</td>
<td><input type="text" class="easyui-textbox" id="txtVcode" name="txtVcode" /></td>
<td><span id="spanVcode" style="color:red"></span></td>
</tr>
<tr style="height:10px"></tr>
<tr>
<td style="width:20px"></td>
<td><img id="image" src="/Login/ValidateCode/?id=1" style="float: left; height: 24px;" /></td>
<td><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" id="changeVcode">看不清,換一張</a></td>
</tr>
</table>
</form>
</div>
<div style="height:10px"></div>
<div data-options="region:'south',border:false" style="text-align:center;padding:5px 0 0;">
<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" id="btnOk" style="width:80px">登錄</a>
<span id="spanCheak" style="color:red"></span>
</div>
</body>
</html>
在上面的代碼可以看出我是用的ajax異步提交表單到了 /Login/CheckLogin 路徑下這代表 Login控制器下的CheckLogin (需要自己新建) 也就是說登錄的邏輯判斷處理就是在CheckLogin 里面完成的。
public ActionResult CheckLogin()
{
//拿到session的值
string Vcode = Session["validateCode"].ToString();
//清空session
Session["validateCode"] = null;
string requestCode = Request["txtVcode"].ToString();
string userName = Request["txtName"].ToString();
string userPwd = Request["txtPwd"].ToString();
if (!requestCode.Equals(Vcode,StringComparison.CurrentCultureIgnoreCase))
{
return Content("no:驗證碼錯誤!!");
}
BLL.UserInfoServices userInfoServices = new BLL.UserInfoServices();
UserInfo userinfo = userInfoServices.GetUserInfoModel(userName, userPwd);
if (userinfo != null)
{
Session["userName"] = userinfo.UserName;
return Content("ok:登錄成功");
}
else
{
return Content("no:用戶名或者密碼錯誤");
}
}
注:連接數(shù)據(jù)庫的語句自己在配置文件里面配置
上面的步驟每步都正確的話,那么重新生成解決方案后,運行輸入用戶名密碼就可以登錄成功了

這里可以看到頁面已經(jīng)實現(xiàn)了跳轉(zhuǎn)。只是主頁還沒有建立而已。下一講就講主頁的布局。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- MVC+EasyUI+三層新聞網(wǎng)站建立 建站準備工作(一)
- MVC+EasyUI+三層新聞網(wǎng)站建立 主頁布局的方法(五)
- MVC+EasyUI+三層新聞網(wǎng)站建立 后臺登錄界面的搭建(二)
- MVC+EasyUI+三層新聞網(wǎng)站建立 驗證碼生成(三)
- 一步步打造簡單的MVC電商網(wǎng)站BooksStore(2)
- 一步步打造簡單的MVC電商網(wǎng)站BooksStore(1)
- MVC4制作網(wǎng)站教程第四章 更新欄目4.3
- MVC4制作網(wǎng)站教程第四章 瀏覽欄目4.2
- MVC4制作網(wǎng)站教程第四章 添加欄目4.1
- MVC+EasyUI+三層新聞網(wǎng)站建立 tabs標(biāo)簽制作方法(六)
相關(guān)文章
詳解Asp.net web.config customErrors 如何設(shè)置
這篇文章主要介紹了詳解Asp.net web.config customErrors 如何設(shè)置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
asp.net利用存儲過程和div+css實現(xiàn)分頁(類似于博客園首頁分頁)
怎么用aspnetpager.dll這個插件對服務(wù)器控件進行分頁,今天與我大家分享一下asp.net利用存儲過程和div+css實現(xiàn)分頁(類似于博客園首頁分頁)2012-01-01
.Net Core使用OpenXML導(dǎo)出、導(dǎo)入Excel
這篇文章主要為大家詳細介紹了.Net Core使用OpenXML導(dǎo)出、導(dǎo)入Excel的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
.net core如何利用ConcurrentTest組件對方法進行壓力測試詳解
這篇文章主要給大家介紹了關(guān)于.net core如何利用ConcurrentTest組件對方法進行壓力測試的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-11-11
asp.net下Response.ContentType類型匯總
asp.net下Response.ContentType類型匯總...2007-04-04
Asp.Net Core輕量級Aop解決方案:AspectCore
這篇文章主要介紹了Asp.Net Core輕量級Aop解決方案:AspectCore,需要的朋友可以參考下2017-06-06
ASP.NET MVC中使用JavaScriptResult的用法示例
這篇文章主要介紹了ASP.NET MVC中使用JavaScriptResult的用法,結(jié)合實例形式分析了采用javascript動態(tài)設(shè)置標(biāo)簽樣式以及使用MVC中的JavaScriptResult來實現(xiàn)同樣效果的相關(guān)技巧,需要的朋友可以參考下2016-08-08

