最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

MVC4制作網(wǎng)站教程第二章 用戶登陸2.2

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:36:41
文檔

MVC4制作網(wǎng)站教程第二章 用戶登陸2.2

MVC4制作網(wǎng)站教程第二章 用戶登陸2.2:一用戶 1.1用戶注冊(cè) 1.2用戶登陸 首先在Models里添加用戶登陸模型類UserLogin,該類只要用用戶名,密碼和驗(yàn)證碼三個(gè)字段。 /// <summary> /// 用戶登陸模型 /// </summary> public class UserLogin { /
推薦度:
導(dǎo)讀MVC4制作網(wǎng)站教程第二章 用戶登陸2.2:一用戶 1.1用戶注冊(cè) 1.2用戶登陸 首先在Models里添加用戶登陸模型類UserLogin,該類只要用用戶名,密碼和驗(yàn)證碼三個(gè)字段。 /// <summary> /// 用戶登陸模型 /// </summary> public class UserLogin { /

一用戶 
1.1用戶注冊(cè) 
1.2用戶登陸 

首先在Models里添加用戶登陸模型類UserLogin,該類只要用用戶名,密碼和驗(yàn)證碼三個(gè)字段。 

/// <summary>
 /// 用戶登陸模型
 /// </summary>
 public class UserLogin
 {
 /// <summary>
 /// 用戶名
 /// </summary>
 [Display(Name = "用戶名", Description = "4-20個(gè)字符。")]
 [Required(ErrorMessage = "×")]
 [StringLength(20, MinimumLength = 4, ErrorMessage = "×")]
 public string UserName { get; set; }
 /// <summary>
 /// 密碼
 /// </summary>
 [Display(Name = "密碼", Description = "6-20個(gè)字符。")]
 [Required(ErrorMessage = "×")]
 [StringLength(20, MinimumLength = 6, ErrorMessage = "×")]
 [DataType(DataType.Password)]
 public string Password { get; set; }
 /// <summary>
 /// 驗(yàn)證碼
 /// </summary>
 [Display(Name = "驗(yàn)證碼", Description = "請(qǐng)輸入圖片中的驗(yàn)證碼。")]
 [Required(ErrorMessage = "×")]
 [StringLength(6, MinimumLength = 6, ErrorMessage = "×")]
 public string VerificationCode { get; set; }

 }

在UserController里添加Login action; 代碼看如下:

public ActionResult Login()
 {
 return View();
 }
 [HttpPost]
 public ActionResult Login(UserLogin login)
 {
 return View();
 }

使用Cookie保存登陸賬號(hào),密碼等信息,修改public ActionResult Login(UserLogin login)。修改完成代碼如下:

[HttpPost]
 public ActionResult Login(UserLogin login)
 {
 //驗(yàn)證驗(yàn)證碼
 if (Session["VerificationCode"] == null || Session["VerificationCode"].ToString() == "")
 {
 Error _e = new Error { Title = "驗(yàn)證碼不存在", Details = "在用戶注冊(cè)時(shí),服務(wù)器端的驗(yàn)證碼為空,或向服務(wù)器提交的驗(yàn)證碼為空", Cause = "<li>你注冊(cè)時(shí)在注冊(cè)頁(yè)面停留的時(shí)間過(guò)久頁(yè)已經(jīng)超時(shí)</li><li>您繞開(kāi)客戶端驗(yàn)證向服務(wù)器提交數(shù)據(jù)</li>", Solution = "返回<a href='" + Url.Action("Register", "User") + "'>注冊(cè)</a>頁(yè)面,刷新后重新注冊(cè)" };
 return RedirectToAction("Error", "Prompt", _e);
 }
 else if (Session["VerificationCode"].ToString() != login.VerificationCode.ToUpper())
 {
 ModelState.AddModelError("VerificationCode", "×");
 return View();
 }
 //驗(yàn)證賬號(hào)密碼
 userRsy = new UserRepository();
 if (userRsy.Authentication(login.UserName, Common.Text.Sha256(login.Password)) == 0)
 {
 HttpCookie _cookie = new HttpCookie("User");
 _cookie.Values.Add("UserName", login.UserName);
 _cookie.Values.Add("Password", Common.Text.Sha256(login.Password));
 Response.Cookies.Add(_cookie);
 return RedirectToAction("Default","User");
 }
 else
 {
 ModelState.AddModelError("Message", "登陸失??!");
 return View();
 }

 }

在public ActionResult Login() 上右鍵添加強(qiáng)類型視圖

完成后代的Login.cshtml 

@model CMS.Models.UserLogin

@{
 ViewBag.Title = "用戶登陸";
 Layout = "~/Views/Shared/_Layout.cshtml";
}
 
<div class="banner"> 
 <img src="~/Skins/Default/Images/banner.jpg" /> 
</div>
 

@using (Html.BeginForm()) 
{ 
 @Html.ValidationSummary(true)

 <div class="form"> 
 <dl> 
 <dt>用戶登陸</dt> 
 <dd> 
 <div class="label">@Html.LabelFor(model => model.UserName):</div> 
 <div class="ctrl">@Html.EditorFor(model => model.UserName) 
 @Html.ValidationMessageFor(model => model.UserName) 
 @Html.DisplayDescriptionFor(model => model.UserName) 
 </div> 
 </dd> 
 <dd> 
 <div class="label">@Html.LabelFor(model => model.Password):</div> 
 <div class="ctrl">@Html.PasswordFor(model => model.Password) 
 @Html.ValidationMessageFor(model => model.Password) 
 @Html.DisplayDescriptionFor(model => model.Password) 
 </div> 
 </dd> 
 <dd> 
 <div class="label">驗(yàn)證碼:</div> 
 <div class="ctrl">
 @Html.TextBoxFor(model => model.VerificationCode) 
 @Html.ValidationMessageFor(model => model.VerificationCode) 
 <img id="verificationcode" alt="" src="@Url.Action("VerificationCode", "User")" /> 
 <a id="trydifferent" style="cursor: pointer">換一張</a> 
 </div> 
 </dd> 
 <dd> 
 <div class="label"></div> 
 <div class="ctrl"> 
 <input type="submit" value="登陸" />@Html.ValidationMessage("Message"); 
 </div> 
 </dd> 
 </dl> 
 <div class="clear"></div> 
 </div>
}

<script type="text/javascript">
 $("#trydifferent").click(function () { 
 $("#verificationcode").attr("src", "/User/VerificationCode?" + new Date()); 
 })

</script>
@section Scripts { 
 @Scripts.Render("~/bundles/jqueryval") 
}

瀏覽器中查看一下登陸頁(yè)面

點(diǎn)下登陸測(cè)試一下。OK登陸成功 

驗(yàn)證用戶是否已經(jīng)登陸,這塊和權(quán)限驗(yàn)證一起從AuthorizeAttribute繼承個(gè)自定義驗(yàn)證類 

在項(xiàng)目里添加Extensions文件夾,添加一個(gè)類UserAuthorizeAttribute 繼承自AuthorizeAttribute,重寫(xiě)AuthorizeCore方法用來(lái)實(shí)現(xiàn)用戶是否已經(jīng)登陸的驗(yàn)證,權(quán)限驗(yàn)證在寫(xiě)權(quán)限功能時(shí)在補(bǔ)充 

using Ninesky.Repository;

namespace System.Web.Mvc
{
 /// <summary>
 /// 用戶權(quán)限驗(yàn)證
 /// </summary>
 public class UserAuthorizeAttribute :AuthorizeAttribute
 {
 /// <summary>
 /// 核心【驗(yàn)證用戶是否登陸】
 /// </summary>
 /// <param name="httpContext"></param>
 /// <returns></returns>
 protected override bool AuthorizeCore(HttpContextBase httpContext)
 {
 //檢查Cookies["User"]是否存在
 if (httpContext.Request.Cookies["User"] == null) return false;
 //驗(yàn)證用戶名密碼是否正確
 HttpCookie _cookie = httpContext.Request.Cookies["User"];
 string _userName = _cookie["UserName"];
 string _password = _cookie["Password"];
 httpContext.Response.Write("用戶名:"+_userName);
 if (_userName == "" || _password == "") return false;
 UserRepository _userRsy = new UserRepository();
 if (_userRsy.Authentication(_userName, _password) == 0) return true;
 else return false;
 }
 }
}

以后只要在需要登陸后才能操作的Action或Controller上加[UserAuthorize]就可實(shí)現(xiàn)驗(yàn)證是否已經(jīng)登錄了。
退出功能,在UserController添加Logout Action 

/// <summary>
 /// 退出系統(tǒng)
 /// </summary>
 /// <returns></returns>
 public ActionResult Logout()
 {
 if (Request.Cookies["User"] != null)
 {
 HttpCookie _cookie = Request.Cookies["User"];
 _cookie.Expires = DateTime.Now.AddHours(-1);
 Response.Cookies.Add(_cookie);
 }
 Notice _n = new Notice { Title = "成功退出", Details = "您已經(jīng)成功退出!", DwellTime = 5, NavigationName="網(wǎng)站首頁(yè)", NavigationUrl = Url.Action("Index", "Home") };
 return RedirectToAction("Notice", "Prompt", _n);
 }

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MVC4制作網(wǎng)站教程第二章 用戶登陸2.2

MVC4制作網(wǎng)站教程第二章 用戶登陸2.2:一用戶 1.1用戶注冊(cè) 1.2用戶登陸 首先在Models里添加用戶登陸模型類UserLogin,該類只要用用戶名,密碼和驗(yàn)證碼三個(gè)字段。 /// <summary> /// 用戶登陸模型 /// </summary> public class UserLogin { /
推薦度:
標(biāo)簽: 用戶 2.2 用戶登陸
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專題
Top