我正在尝试创建一个具有管理部分的web应用程序,在那里可以使用MVC、razor和实体框架(代码优先)查看和编辑帐户信息。我的问题是,当我尝试使用控制器重置密码时,其余的帐户信息会被删除。这是我的控制器
// GET: Edit/5
public ActionResult Edit(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ApplicationUser applicationUser = db.Users.Find(id);
if (applicationUser == null)
{
return HttpNotFound();
}
return View(applicationUser);
}
// POST: Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id, EmployeeNumber, FirstName, LastName, Department, Supervisor, Email, UserName, Password, Confirm Password")] ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(applicationUser.Email);
applicationUser.SecurityStamp = Guid.NewGuid().ToString("D");
string resetToken = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var pass = Request["Password"];
IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(user.Id, resetToken, Request["Password"]);
db.Entry(applicationUser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(applicationUser);
}
当调试和单步执行代码并查看数据库时,我看到密码被散列并正确存储,直到db.SaveChanges();
,在单步执行完这一行之后,密码被删除,但其余的信息被保存。因此,我认为也许将密码重置过程移到该行之后会有所帮助,但随后将删除编辑的额外信息,同时保存密码。所以我似乎找不到为什么会这样。我找到了其他帖子,但他们说解决方案是要么对令牌进行编码,用'+‘替换空格,要么给它加时间戳,但从这些帖子看,我写的重置过程似乎是正确的,而且我似乎遇到了一点不同的情况。令牌验证也同样成功,并且在整个过程中都会保留时间戳。也许有人可以告诉我为什么我会有这样的问题?非常感谢!
我使用的是ApplicationUser模型,如下所示。
namespace ReconciliationApp.Models
{
public class ApplicationUser : IdentityUser
{
public string EmployeeNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set; }
public string Supervisor { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("ReconciliationContext")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public System.Data.Entity.DbSet<ReconciliationApp.Models.RegisterViewModel> RegisterViewModels { get; set; }
}
}
如果你需要更多相关的信息,也请让我知道!
发布于 2016-07-22 16:48:55
我想明白了,似乎我只需要单独设置所有内容,而不是一次性设置所有内容。我想我只是想把这个控制器建立在我拥有和使用的一些脚手架控制器的基础上。这是我的工作代码。
// POST: CSReconForms/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id, EmployeeNumber, FirstName, LastName, Department, Supervisor, Email, UserName, Password, Confirm Password")] ApplicationUser applicationUser)
{
if (ModelState.IsValid)
{
//UserManager.Create<applicationu>();
var user = await UserManager.FindByNameAsync(applicationUser.Email);
//applicationUser.SecurityStamp = Guid.NewGuid().ToString("D");
user.EmployeeNumber = applicationUser.EmployeeNumber;
user.FirstName = applicationUser.FirstName;
user.LastName = applicationUser.LastName;
user.Department = applicationUser.Department;
user.Supervisor = applicationUser.Supervisor;
string resetToken = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var pass = Request["Password"];
IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(user.Id, resetToken, Request["Password"]);
return RedirectToAction("Index");
}
return View(applicationUser);
}
https://stackoverflow.com/questions/38516815
复制相似问题