【问题标题】:CreateAsync in User Manager - dispose method always called用户管理器中的 CreateAsync - 始终调用 dispose 方法
【发布时间】:2016-08-01 08:41:34
【问题描述】:

我正在使用 Asp.Net 身份系统,但遇到了 Register 方法的问题,特别是这一行:

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

完整的方法是

[ActionName("Register")]
[AcceptVerbs("GET", "POST")]
[HttpPost]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var user = new ApplicationUser() {UserName = model.Email, Email = model.Email};

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok();
}

我从来没有得到“结果”,而是总是使用 Dispose() 方法

protected override void Dispose(bool disposing)
{
   if (disposing && _userManager != null)
   {
     _userManager.Dispose();
      _userManager = null;
   }
   base.Dispose(disposing);
}

我错过了什么?

【问题讨论】:

  • 请给我们看完整的代码
  • 我想你正在使用实体框架。您是否为创建/更新数据库创建了迁移?

标签: c# asp.net oauth-2.0 asp.net-identity idisposable


【解决方案1】:

我在我的 Web 应用程序中使用 Code-First,我试图自定义 Identity 的 ApplicationUser,但在 Identity 的 ApplicationUser 类中添加关系后,我忘记了删除或更新我的数据库。 所以,我实际上做的是这样的……

 public class ApplicationUser : IdentityUser
{
    [Required()]
    public string FullName { get; set; }
    public byte[] UserPhoto { get; set; }

    public virtual List<Class> Classes { 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 ApplicationUser : IdentityUser
{
    [Required()]
    public string FullName { get; set; }
    public byte[] UserPhoto { get; set; }

    //public virtual List<Class> Classes { 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;
    }
}

【讨论】:

    猜你喜欢
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    相关资源
    最近更新 更多