一用戶(hù)
1.1用戶(hù)注冊(cè)
1.2用戶(hù)登錄
1.3修改密碼
修改密碼會(huì)用到一個(gè)UserChangePassword 模型類(lèi),先添加UserChangePassword類(lèi)
/// summary>
/// 用戶(hù)修改密碼模型
/// /summary>
[NotMapped]
public class UserChangePassword
{
/// summary>
/// 原密碼
/// /summary>
[Display(Name = "原密碼")]
[Required(ErrorMessage = "×")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "×")]
[DataType(DataType.Password)]
public string Password { get; set; }
/// summary>
/// 新密碼
/// /summary>
[Display(Name = "新密碼", Description = "6-20個(gè)字符。")]
[Required(ErrorMessage = "×")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "×")]
[DataType(DataType.Password)]
public string NewPassword { get; set; }
/// summary>
/// 確認(rèn)密碼
/// /summary>
[Display(Name = "確認(rèn)密碼", Description = "再次輸入密碼。")]
[Compare("NewPassword", ErrorMessage = "×")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
}
用到了數(shù)據(jù)庫(kù)的更新,先打開(kāi)UserRepository.cs 修改Update函數(shù)
/// summary>
/// 更新用戶(hù)信息
/// /summary>
/// param name="user">/param>
/// returns>/returns>
public override bool Update(User user)
{
dbContext.Users.Attach(user);
dbContext.EntryUser>(user).State = System.Data.EntityState.Modified;
if (dbContext.SaveChanges() > 0) return true;
else return false;
}
在修改密碼時(shí)需要查出用戶(hù)信息并更新,需要添加Find(string UserName) 函數(shù)
/// summary>
/// 查找用戶(hù)
/// /summary>
/// param name="UserName">用戶(hù)名/param>
/// returns>/returns>
public User Find(string UserName)
{
return dbContext.Users.SingleOrDefault(u => u.UserName == UserName);
}
好了打開(kāi)UserController,添加ChangePassword Action
/// summary>
/// 修改密碼
/// /summary>
/// returns>/returns>
[UserAuthorize]
public ActionResult ChangePassword()
{
return View();
}
[HttpPost]
[UserAuthorize]
public ActionResult ChangePassword()
{
return View();
}
添加強(qiáng)類(lèi)型(UserChangePassword)視圖,在視圖中添加三個(gè)文本框,修改后的視圖代碼
@model Ninesky.Models.UserChangePassword
@{
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.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">@Html.LabelFor(model => model.NewPassword):/div>
div class="ctrl">@Html.PasswordFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword)
@Html.DisplayDescriptionFor(model => model.NewPassword)
/div>
/dd>
dd>
div class="label">@Html.LabelFor(model => model.ConfirmPassword):/div>
div class="ctrl">@Html.PasswordFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
@Html.DisplayDescriptionFor(model => model.ConfirmPassword)
/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>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
修改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)查詢(xún)不到用戶(hù)信息", Cause = Server.UrlEncode("li>用戶(hù)在修改密碼界面停留的時(shí)間過(guò)長(zhǎng),登錄信息已失效。/li>li>系統(tǒng)錯(cuò)誤。/li>"), Solution = Server.UrlEncode("li>返回a href='" + Url.Action("ChangePassword", "User") + "'>修改密碼/a>頁(yè)面,輸入正確的信息后重新注冊(cè)/li>li>聯(lián)系網(wǎng)站管理員/li>") };
return RedirectToAction("Error", "Prompt", _e);
}
_user.Password = Common.Text.Sha256(userChangePassword.NewPassword);
if (userRsy.Update(_user))
{
Notice _n = new Notice { Title = "成功修改密碼", Details = "您已經(jīng)成功修改密碼,請(qǐng)牢記您的新密碼!", DwellTime = 5, NavigationName = "登陸頁(yè)面", NavigationUrl = Url.Action("Login", "User") };
return RedirectToAction("Notice", "Prompt", _n);
}
else
{
Error _e = new Error { Title = "修改密碼失敗", Details = "修改密碼時(shí),更新數(shù)據(jù)庫(kù)失??!", Cause = Server.UrlEncode("li>系統(tǒng)錯(cuò)誤。/li>"), Solution = Server.UrlEncode("li>返回a href='" + Url.Action("ChangePassword", "User") + "'>修改密碼/a>頁(yè)面,輸入正確的信息后重新注冊(cè)/li>li>聯(lián)系網(wǎng)站管理員/li>") };
return RedirectToAction("Error", "Prompt", _e);
}
}
else
{
ModelState.AddModelError("Password", "原密碼不正確,請(qǐng)重新輸入");
return View();
}
}
在上面的代碼中用到了一個(gè)UserName屬性,這個(gè)是在UserController添加的屬性用于返回Cookie中保存的用戶(hù)名
/// summary>
/// 獲取用戶(hù)名
/// /summary>
public string UserName {
get {
HttpCookie _cookie = Request.Cookies["User"];
if (_cookie == null) return "";
else return _cookie["UserName"];
}
}
好了,瀏覽器中預(yù)覽一下
測(cè)試一下,OK,大功告成!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)登錄、注銷(xiāo)(五)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)注冊(cè)(四)
- ASP.NET MVC5 網(wǎng)站開(kāi)發(fā)框架模型、數(shù)據(jù)存儲(chǔ)、業(yè)務(wù)邏輯(三)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)項(xiàng)目框架(二)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)顯示文章列表(九)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)修改及刪除文章(十)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)管理列表、回復(fù)及刪除(十三)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)修改資料和密碼(六)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)咨詢(xún)管理的架構(gòu)(十一)
- ASP.NET MVC5網(wǎng)站開(kāi)發(fā)我的咨詢(xún)列表及添加咨詢(xún)(十二)