首页 文章

正在删除帐户信息,并在用户控制器中通过编辑重置密码

提问于
浏览
1

我正在尝试使用管理部分创建一个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(); ,然后在单步执行该密码后删除密码,但保存其余信息 . 所以我想可能会将密码重置过程移动到该行有用之后,但随后会删除已编辑的额外信息,同时保存密码 . 所以我可以't seem to find out why this is the case. I'找到其他帖子,但是他们说解决方案是要么编码令牌,用'+'替换空格或者给它加时间戳,但是从读取那些似乎重置过程我有问题我''我有吗?这将不胜感激!

我正在使用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; }
    }
}

如果您还需要更多可能与之相关的信息,请与我们联系!

1 回答

  • 0

    我想通了,似乎我只需要单独设置所有内容而不是一次性设置 . 我想我只是想把这个控制器放在我拥有和使用的一些脚手架控制器上 . 这是我的工作代码 .

    // 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);
        }
    

相关问题