0

I realized that my register async function that was created by default stopped working after I moved my IdentityModel to a Class Library and made my application reference it.

I based my changes on this link Moving ASP.NET Identity model to class library

Currently my DbContext and Migrations all sit within my class library with my Application holding the connection string. Have I missed something when moving it across?

This is the register function in my main application:

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

This is my IdentityModel in the class library

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

Edit

This is the error

enter image description here

Edit 2

This is my connection string

<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>
halfer
  • 19,824
  • 17
  • 99
  • 186
JianYA
  • 2,750
  • 8
  • 60
  • 136

1 Answers1

0

The problem seems to be the path to the database is wrong. Either the path is too long or contains an invalid character. Check your connection string.

Neil
  • 11,059
  • 3
  • 31
  • 56
  • I've added my connection string. This sits in my Application Web.config. – JianYA Aug 04 '18 at 08:19
  • Does that mdf file exist in the specified folder ? Do you need to create it or run 'update-database' ? – Neil Aug 04 '18 at 08:22
  • Update-database is run in the class library? I think the mdf exists in the class library as well. – JianYA Aug 04 '18 at 08:25
  • You can run update-database from the package manager console. Use file manager to check the mdf exists in the path given in the error message ! – Neil Aug 04 '18 at 08:28
  • That's strange. I added the folder App_Data and my register worked. Nothing exists in that folder? – JianYA Aug 04 '18 at 08:31
  • So,we can conclude from this that the paths are not automatically created by the code that creates the database. I think you will find in your original project that app data folder is created as part of the build process and you have since deleted it from the project or not copied it to you class library ? – Neil Aug 04 '18 at 08:34
  • I deleted it from my application project which was a mistake. I did not realize that even if I moved my dbcontext to a class library, the mdf file would still be stored in the local application. – JianYA Aug 04 '18 at 08:37