【问题标题】:ChangepasswordAsync not work更改密码异步不起作用
【发布时间】:2016-12-11 12:37:09
【问题描述】:

我使用此代码用于管理面板由经理更改客户的密码,我没有收到任何错误。但密码没有改变, 我从this 得到了我的男女同校

 [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual  ActionResult ChangeUserPassword(SetPasswordViewModel model,string userId)
    {
        if (ModelState.IsValid)
        {
            //var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
            ApplicationUser appUser = db.Users.Find(userId);
            var result =  UserManager.ChangePasswordAsync(appUser, model.NewPassword);
           if (result.IsCompleted)
            {
                var user =  UserManager.FindById(User.Identity.GetUserId());
                //var user = db.Users.Find(userId);
                if (user != null)
                {
                    //await SignInManager<,>.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                }
                return RedirectToAction("Index", new { Message = ManageController.ManageMessageId.SetPasswordSuccess });
            }
            // AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

}

在控制器中使用这个

 public async Task<IdentityResult> ChangePasswordAsync(ApplicationUser Appuser, string newPassword)
    {
        var store = this.Store as Microsoft.AspNet.Identity.IUserPasswordStore<ApplicationUser,string>;
        if (store == null)
        {
            var errors = new string[]
            {
            "Current UserStore doesn't implement IUserPasswordStore"
            };
            return IdentityResult.Failed(errors);
            // return Task.FromResult(new IdentityResult(errors) { Succeeded = false });

        }

        var newPasswordHash = this.PasswordHasher.HashPassword(newPassword);

        await store.SetPasswordHashAsync(Appuser, newPasswordHash);
        await store.UpdateAsync(Appuser);

        //return await Task.FromResult<IdentityResult>(IdentityResult.Success);
        return IdentityResult.Success;
    }
}

我的错误是什么?

更新答案后我改用这个方法

 [HttpPost]
    public async Task<IdentityResult> ChangePasswordAsync(ApplicationUser appuserId, string newPassword)
    {
        ApplicationDbContext db = new ApplicationDbContext();
        //var userr =await db.Users.FirstOrDefaultAsync(x => x.Id == appuserId.Id);

        var newPasswordHash = this.PasswordHasher.HashPassword(newPassword);

        db.Entry(Users).State = EntityState.Modified;
        if (appuserId != null) appuserId.PasswordHash = newPasswordHash;
        db.SaveChanges();
        return IdentityResult.Success;
    }

但又遇到了同样的问题

在我拥有的 IdentityModels.cs 中

 public DbSet<CommonAction> CommonActions { get; set; }
    public DbSet<PublicContent> PublicContents { get; set; }
    public DbSet<MainSubject> MainSubjects { get; set; }
    public DbSet<EducationLevel> EducationLevels { get; set; }
    public DbSet<TimePeriod> TimePeriods { get; set; }
    public DbSet<HelpType> HelpTypes { get; set; }
    public DbSet<FinancialSupport> FinancialSupports { get; set; }
    public DbSet<LinksStatistic> LinksStatistics { get; set; }
    public DbSet<SlideOfCommonAction> SlideOfCommonActions { get; set; }

在普通模型中 IdentityModel 用户未注册为 DbSet ()

【问题讨论】:

  • 想详细说明否决票?
  • db.Entry(Users).State 行中的对象Users 是什么?
  • 它与 ApplicationUser 相同,如果我添加此public DbSet&lt;ApplicationUser&gt; Users { get; set; },则意味着我收到此错误Multiple object sets per type are not supported. The object sets 'Users' and 'Users' can both contain instances of type 'NGO.Models.ApplicationUser'.

标签: c# asp.net asp.net-mvc identity


【解决方案1】:

您不是在等待ChangePasswordAsync,您只是在检查它是否已完成。这可能会导致在更改密码之前返回视图。

那么,如果可能的话,你应该尝试一直使用 async/await。这意味着您的操作也应该是异步的。

 [HttpPost]
    [ValidateAntiForgeryToken]
    public virtual async Task<ActionResult> ChangeUserPassword(SetPasswordViewModel model,string userId)
    {
        if (ModelState.IsValid)
        {
            //var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
            ApplicationUser appUser = db.Users.Find(userId);

            // await the result.
            var result = await UserManager.ChangePasswordAsync(appUser, model.NewPassword);
           if (result.Succeeded) // Or whatever you're expecting.
            {
                var user =  UserManager.FindById(User.Identity.GetUserId());
                //var user = db.Users.Find(userId);
                if (user != null)
                {
                    //await SignInManager<,>.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                }
                return RedirectToAction("Index", new { Message = ManageController.ManageMessageId.SetPasswordSuccess });
            }
            // AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

}

cmets 后更新:

实体类型 DbSet`1 不是当前上下文模型的一部分

db.Entry(Users).State = EntityState.Modified;

这是 EF 无法将您的模型连接到上下文中的实体的情况。没有看到代码,很难说出确切的原因。但可能是您在上下文中缺少DbSet

您是否正确配置了您的UserManager?其他UserManager 操作是否有效?

尝试调试,看看它在哪里崩溃。

【讨论】:

  • 谢谢,没关系,但是当我点击更改密码时出现新错误
  • “实体类型 DbSet`1 不是当前上下文模型的一部分。”关于“ db.Entry(Users).State = EntityState.Modified;”
  • 完成后我更改了主要问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2015-08-06
  • 2013-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 2018-02-14
相关资源
最近更新 更多