asp.net-mvc - 如何在mvc中更新登录密码?
问题描述
我想更新登录密码
当用户登录然后看到他们自己的个人资料后,我在我的应用程序中做什么
学生.cs
namespace Demo.Models
{
public class student
{
public int studentid { get; set; }
[Display(Name = "FirstName")]
public string firstname { get; set; }
[Display(Name = "LastName")]
public string lastname { get; set; }
[Display(Name = "UserName")]
public string username { get; set; }
[Display(Name = "Password")]
public string password { get; set; }
[Display(Name = "Emailid")]
public string emailid { get; set; }
}
家庭控制器.cs
public ActionResult Index()
{
if (Session["username"] == null)
{
return RedirectToAction("Login", "Home");
}
else
{
return View(getcurrentstudent());
}
}
public student getcurrentstudent()
{
var currentusername = Session["username"].ToString();
var currentpassword = Session["password"].ToString();
var currentstu = _dbcontextstud.stud.Where(s => s.username == currentusername && s.password == currentpassword).SingleOrDefault();
return currentstu;
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(student stud)
{
var login = _dbcontextstud.stud.Where(x => x.username == stud.username && x.password == stud.password).FirstOrDefault();
if (login != null)
{
Session["username"] = login.username.ToString();
Session["password"] = login.password.ToString();
return RedirectToAction("Index");
}
return RedirectToAction("Login");
}
public ActionResult create()
{
return View();
}
[HttpPost]
public ActionResult create(student stud)
{
var create = _dbcontextstud.stud.Add(stud);
_dbcontextstud.SaveChanges();
return RedirectToAction("Login");
}
索引.cshtml
@model Demo.Models.student
<h2>Index</h2>
@if (Session["username"] != null)
{
<text>Welcome <strong>@Session["username"].ToString()</strong></text>
}
<p>
@Html.ActionLink("Logout", "Logout")
</p>
<table class="table">
<tr>
<td>
@Html.DisplayFor(model => model.firstname)
</td>
<td>
@Html.DisplayFor(model => model.lastname)
</td>
<td>
@Html.DisplayFor(model => model.username)
</td>
<td>
@Html.DisplayFor(model => model.password)
</td>
<td>
@Html.DisplayFor(model => model.emailid)
</td>
</tr>
</table>
数据库图:
登录时查看参考:
如何在 MVC 中更新登录密码或哪些字段额外需要添加表来更新密码或更新密码的逻辑实现?
请帮忙
解决方案
在您的情况下,您只需要使用实体框架更新数据库属性。
从数据库中获取项目:
_dbcontextstud.stud.FirstOrDefault()
更改项目属性:
user.Password = model.NewPassword
保存更改:
_dbcontextstud.SaveChanges()
例子:
[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model)
{
var currentUsername = Session["username"].ToString();
var currentPassword = model.CurrentPassword;
var user = _dbcontextstud.stud.FirstOrDefault(s => s.username == currentUsername && s.password == currentPassword);
if(user == null)
{
// user password did not match the DB password
throw new Exception("Invalid password exception");
}
// update the new password
user.Password = model.NewPassword;
// save the Database changes
_dbcontextstud.SaveChanges();
return RedirectToAction("Index");
}
// view model
public class ChangePasswordViewModel
{
[Required]
public string CurrentPassword { get; set; }
[Required]
public string NewPassword { get; set; }
}
推荐阅读
- jupyter-notebook - TypeError: stat: path 应该是字符串、字节、os.PathLike 或整数,而不是 TrainingResult
- r - 在 R 中使用 dplyr 替换时间点
- python-3.x - 我无法弄清楚为什么它会打印出已从列表中删除的数字
- python - python tkinter中的按钮被按住检测
- typescript - TypeScript - 函数断言的窄类型
- python - 如何掷骰子
- r - 给定 R 中的一组约束,如何优化多个目标/变量?
- android - 带有 RxJava3 的房间
- html - 我可以根据特定的数组名动态检查当前的单选按钮数组吗?
- javascript - 如何按对象键和值过滤对象?