将身份模型和dbcontext移动到类库后,注册Async不起作用

时间:2018-08-04 07:23:13

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

我意识到,在将IdentityModel移到类库并让我的应用程序引用它之后,默认创建的注册异步函数停止工作。

我基于此链接Moving ASP.NET Identity model to class library

进行了更改

目前,我的DbContext和Migrations都位于类库中,而我的应用程序则持有连接字符串。移动时我错过了什么吗?

这是我主要应用程序中的注册功能:

// POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    var ued = new AspNetUsersExtendedDetails
                    {
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Address = "",
                        Notes = "",
                        UserId = user.Id
                    };
                    RegisterExtendedDetails(ued);
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

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

这是我在类库中的IdentityModel

public class ApplicationUser : IdentityUser
    {
        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 virtual DbSet<AspNetUsersExtendedDetails> AspNetUsersExtendedDetails { get; set; }
        public virtual DbSet<AspNetApplications> AspNetApplications { get; set; }
        public virtual DbSet<AspNetEventLogs> AspNetEventLogs { get; set; }
        public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
        {

        }
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

编辑

这是错误

enter image description here

编辑2

这是我的连接字符串

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-MSAPP-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MSAPP-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

1 个答案:

答案 0 :(得分:0)

问题似乎是数据库的路径错误。路径太长或包含无效字符。检查您的连接字符串。