当前位置: 移动技术网 > IT编程>开发语言>.net > MVC4制作网站教程第二章 用户密码修改2.3

MVC4制作网站教程第二章 用户密码修改2.3

2017年12月12日  | 移动技术网IT编程  | 我要评论

淘宝网化妆包,乌台诗案的受害人,监狱风云之夜囚

一用户
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.entry<user>(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,大功告成!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网