Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logic doesn't transfer to new Mvc5 webapps VS2013 update4 for Individual User Accounts #47

Open
0xMF opened this issue May 27, 2015 · 1 comment
Labels

Comments

@0xMF
Copy link

0xMF commented May 27, 2015

When creating a new Mvc5 webapp in VS2013 Update 4 with Individual User Accounts. The wizard generated IdentityConfig, AccountController, and ManageController are very different from this sample. This means the logic for seeding doesn't work.

public class MyDbInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
    {
        protected override void Seed(ApplicationDbContext context)
        {
            InitializeIdentityForEF(context);
            InitializeMyData(context);
        }

private void InitializeIdentityForEF(ApplicationDbContext context)
        {
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));


            string name = "Admin";
            string password = "May-27-2015";

            //Create Role Admin if it does not exist
            if (!RoleManager.RoleExists(name))
            {
                var roleresult = RoleManager.Create(new IdentityRole(name));
            }

            //Create admin=Admin with password=123456
            var admin = new ApplicationUser();
            admin.UserName = name;
            admin.Email = "[email protected]";
            admin.EmailConfirmed = true;
            admin.PhoneNumber = "5551234567";
            admin.PhoneNumberConfirmed= true;
            var adminresult = UserManager.Create(admin, password);

            //Add admin Admin to Role Admin
            if (adminresult.Succeeded)
            {
                var result = UserManager.AddToRole(admin.Id, name);
            }
        }

private void InitializeMyData(ApplicationDbContext dc)
        {
            // Custom data initizations
        }
}
  • this sample code given in this example works fine in VS2013 update 4 but doing File New Mvc5 webapp + Individual Accounts no longer works if I attempt to seed with the logic shown above
  • cannot login as "Admin" (specifically cannot login as [email protected] on the Login page)
  • registering a new user logs in but after logging out I cannot login with the new user credentials
@0xMF
Copy link
Author

0xMF commented May 27, 2015

I think I solved this issue for the given seeding values. At minimum, you'd need to initialize the database such that UserName have an email-style string that would be used at logon:

          // Username must be an email-style string for Individual Accounts in Identity v2.1+
          // otherwise you cannot login

            var admin = new ApplicationUser();
            admin.UserName = "[email protected]";
            admin.Email = "[email protected]";
            var adminresult = UserManager.Create(admin, password);

More testing would be needed for other changes but at least logging in via seeded values is possible now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants