一用户
1.1用户注册
1.2用户登录
1.3修改密码
修改密码会用到一个UserChangePassword 模型类,先添加UserChangePassword类
/// summary>
/// 用户修改密码模型
/// /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个字符。")]
[Required(ErrorMessage = "×")]
[StringLength(20, MinimumLength = 6, ErrorMessage = "×")]
[DataType(DataType.Password)]
public string NewPassword { get; set; }
/// summary>
/// 确认密码
/// /summary>
[Display(Name = "确认密码", Description = "再次输入密码。")]
[Compare("NewPassword", ErrorMessage = "×")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
}
用到了数据库的更新,先打开UserRepository.cs 修改Update函数
/// summary>
/// 更新用户信息
/// /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;
}
在修改密码时需要查出用户信息并更新,需要添加Find(string UserName) 函数
/// summary>
/// 查找用户
/// /summary>
/// param name="UserName">用户名/param>
/// returns>/returns>
public User Find(string UserName)
{
return dbContext.Users.SingleOrDefault(u => u.UserName == UserName);
}
好了打开UserController,添加ChangePassword Action
/// summary>
/// 修改密码
/// /summary>
/// returns>/returns>
[UserAuthorize]
public ActionResult ChangePassword()
{
return View();
}
[HttpPost]
[UserAuthorize]
public ActionResult ChangePassword()
{
return View();
}
添加强类型(UserChangePassword)视图,在视图中添加三个文本框,修改后的视图代码
@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()以实现修改密码,修改完成的代码如下:
[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 = "修改密码时,系统查询不到用户信息", Cause = Server.UrlEncode("li>用户在修改密码界面停留的时间过长,登录信息已失效。/li>li>系统错误。/li>"), Solution = Server.UrlEncode("li>返回a href='" + Url.Action("ChangePassword", "User") + "'>修改密码/a>页面,输入正确的信息后重新注册/li>li>联系网站管理员/li>") };
return RedirectToAction("Error", "Prompt", _e);
}
_user.Password = Common.Text.Sha256(userChangePassword.NewPassword);
if (userRsy.Update(_user))
{
Notice _n = new Notice { Title = "成功修改密码", Details = "您已经成功修改密码,请牢记您的新密码!", DwellTime = 5, NavigationName = "登陆页面", NavigationUrl = Url.Action("Login", "User") };
return RedirectToAction("Notice", "Prompt", _n);
}
else
{
Error _e = new Error { Title = "修改密码失败", Details = "修改密码时,更新数据库失败!", Cause = Server.UrlEncode("li>系统错误。/li>"), Solution = Server.UrlEncode("li>返回a href='" + Url.Action("ChangePassword", "User") + "'>修改密码/a>页面,输入正确的信息后重新注册/li>li>联系网站管理员/li>") };
return RedirectToAction("Error", "Prompt", _e);
}
}
else
{
ModelState.AddModelError("Password", "原密码不正确,请重新输入");
return View();
}
}
在上面的代码中用到了一个UserName属性,这个是在UserController添加的属性用于返回Cookie中保存的用户名
/// summary>
/// 获取用户名
/// /summary>
public string UserName {
get {
HttpCookie _cookie = Request.Cookies["User"];
if (_cookie == null) return "";
else return _cookie["UserName"];
}
}
好了,浏览器中预览一下
测试一下,OK,大功告成!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:- ASP.NET MVC5网站开发用户登录、注销(五)
- ASP.NET MVC5网站开发用户注册(四)
- ASP.NET MVC5 网站开发框架模型、数据存储、业务逻辑(三)
- ASP.NET MVC5网站开发项目框架(二)
- ASP.NET MVC5网站开发显示文章列表(九)
- ASP.NET MVC5网站开发修改及删除文章(十)
- ASP.NET MVC5网站开发管理列表、回复及删除(十三)
- ASP.NET MVC5网站开发用户修改资料和密码(六)
- ASP.NET MVC5网站开发咨询管理的架构(十一)
- ASP.NET MVC5网站开发我的咨询列表及添加咨询(十二)