這篇文章將為大家詳細(xì)講解有關(guān)MVC4制作網(wǎng)站中怎樣開發(fā)用戶密碼修改,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)服務(wù)團(tuán)隊(duì)是一支充滿著熱情的團(tuán)隊(duì),執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時(shí)竭誠(chéng)為客戶提供服務(wù)是我們的理念。創(chuàng)新互聯(lián)建站把每個(gè)網(wǎng)站當(dāng)做一個(gè)產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!一用戶
1.1用戶注冊(cè)
1.2用戶登錄
1.3修改密碼
修改密碼會(huì)用到一個(gè)UserChangePassword 模型類,先添加UserChangePassword類
////// 用戶修改密碼模型 /// [NotMapped] public class UserChangePassword { ////// 原密碼 /// [Display(Name = "原密碼")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 6, ErrorMessage = "×")] [DataType(DataType.Password)] public string Password { get; set; } ////// 新密碼 /// [Display(Name = "新密碼", Description = "6-20個(gè)字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 6, ErrorMessage = "×")] [DataType(DataType.Password)] public string NewPassword { get; set; } ////// 確認(rèn)密碼 /// [Display(Name = "確認(rèn)密碼", Description = "再次輸入密碼。")] [Compare("NewPassword", ErrorMessage = "×")] [DataType(DataType.Password)] public string ConfirmPassword { get; set; } }
用到了數(shù)據(jù)庫(kù)的更新,先打開UserRepository.cs 修改Update函數(shù)
////// 更新用戶信息 /// /// ///public override bool Update(User user) { dbContext.Users.Attach(user); dbContext.Entry (user).State = System.Data.EntityState.Modified; if (dbContext.SaveChanges() > 0) return true; else return false; }
在修改密碼時(shí)需要查出用戶信息并更新,需要添加Find(string UserName) 函數(shù)
////// 查找用戶 /// /// 用戶名 ///public User Find(string UserName) { return dbContext.Users.SingleOrDefault(u => u.UserName == UserName); }
好了打開UserController,添加ChangePassword Action
////// 修改密碼 /// ///[UserAuthorize] public ActionResult ChangePassword() { return View(); } [HttpPost] [UserAuthorize] public ActionResult ChangePassword() { return View(); }
添加強(qiáng)類型(UserChangePassword)視圖,在視圖中添加三個(gè)文本框,修改后的視圖代碼
@model Ninesky.Models.UserChangePassword @{ ViewBag.Title = "修改密碼"; Layout = "~/Views/Shared/_Layout.cshtml"; }@using (Html.BeginForm()) { @Html.ValidationSummary(true)} @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
- 修改密碼
@Html.LabelFor(model => model.Password):@Html.PasswordFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) @Html.DisplayDescriptionFor(model => model.Password) @Html.LabelFor(model => model.NewPassword):@Html.PasswordFor(model => model.NewPassword) @Html.ValidationMessageFor(model => model.NewPassword) @Html.DisplayDescriptionFor(model => model.NewPassword) @Html.LabelFor(model => model.ConfirmPassword):@Html.PasswordFor(model => model.ConfirmPassword) @Html.ValidationMessageFor(model => model.ConfirmPassword) @Html.DisplayDescriptionFor(model => model.ConfirmPassword) @Html.ValidationMessage("Message")
修改UserController中[HttpPost]方式的ChangePassword()以實(shí)現(xiàn)修改密碼,修改完成的代碼如下:
[HttpPost] [UserAuthorize] public ActionResult ChangePassword(UserChangePassword userChangePassword) { userRsy = new UserRepository(); if (userRsy.Authentication(UserName, Common.Text.Sha256(userChangePassword.Password)) == 0) { var _user = userRsy.Find(UserName); if (_user == null) { Error _e = new Error { Title = "修改密碼失敗", Details = "修改密碼時(shí),系統(tǒng)查詢不到用戶信息", Cause = Server.UrlEncode("
在上面的代碼中用到了一個(gè)UserName屬性,這個(gè)是在UserController添加的屬性用于返回Cookie中保存的用戶名
////// 獲取用戶名 /// public string UserName { get { HttpCookie _cookie = Request.Cookies["User"]; if (_cookie == null) return ""; else return _cookie["UserName"]; } }
好了,瀏覽器中預(yù)覽一下
測(cè)試一下,OK,大功告成!
關(guān)于MVC4制作網(wǎng)站中怎樣開發(fā)用戶密碼修改就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。