一用户
1.1用户注册
1.2用户登录
1.3修改密码
修改密码会用到一个UserChangePassword 模型类,先添加UserChangePassword类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/// <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函数
1
2
3
4
5
6
7
8
9
10
11
12
|
/// <summary>
/// 更新用户信息
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public override bool Update(User user)
{
dbContext.Users.Attach(user);
dbContext.Entry<User>(user).State = System.Data.EntityState.Modified;
if (dbContext.SaveChanges() > 0) return true ;
else return false ;
}
|
在修改密码时需要查出用户信息并更新,需要添加Find(string UserName) 函数
1
2
3
4
5
6
7
8
9
|
/// <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)视图,在视图中添加三个文本框,修改后的视图代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
@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()以实现修改密码,修改完成的代码如下:
在上面的代码中用到了一个UserName属性,这个是在UserController添加的属性用于返回Cookie中保存的用户名
1
2
3
4
5
6
7
8
9
10
|
/// <summary>
/// 获取用户名
/// </summary>
public string UserName {
get {
HttpCookie _cookie = Request.Cookies[ "User" ];
if (_cookie == null ) return "" ;
else return _cookie[ "UserName" ];
}
}
|
好了,浏览器中预览一下
测试一下,OK,大功告成!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/mzwhj/archive/2012/11/01/2748310.html