Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can`t login with seeded database configuration using asp.net identity

I have a problem with login, even though i see the tables are filled with my seeded user info from Configuration.cs:

protected override void Seed(www.Models.ApplicationDbContext context)
        {
            if (!context.Roles.Any(x => x.Name == "admin"))
            {
                var roleStore = new RoleStore<IdentityRole>(context);
                var roleManager = new RoleManager<IdentityRole>(roleStore);
                var role = new IdentityRole { Name = "admin" };
                roleManager.Create(role);
            }

            if (!context.Users.Any(x => x.UserName == "admin" && x.Email == "[email protected]"))
            {
                var userStore = new UserStore<ApplicationUser>(context);
                var userManager = new UserManager<ApplicationUser>(userStore);
                var user = new ApplicationUser { UserName = "admin", Email = "[email protected]" };
                var hasher = new PasswordHasher();
                userManager.Create(user, "MySecret5");
                userManager.AddToRole(user.Id, "admin");
            }
        }

and when i try to login i get error "Invalid login attempt". What am i missing?

EDIT: Im in the process of learning all stuff about asp.net so im pretty big noob now :( so i found this example to be working for me, and if anyone else needs it here it is:

protected override void Seed(www.Models.ApplicationDbContext context)
        {
            var userStore = new UserStore<ApplicationUser>(context);
            var userManager = new UserManager<ApplicationUser>(userStore);

            if (!context.Users.Any(x => x.UserName == "[email protected]"))
            {
                var user = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]" };
                userManager.Create(user, "Password5%");
                context.Roles.AddOrUpdate(x => x.Name, new IdentityRole { Name = "admin" });
                context.SaveChanges();
                userManager.AddToRole(user.Id, "admin");
            }
        }

And thanks for all your help and time.

like image 529
john Avatar asked Sep 14 '15 21:09

john


Video Answer


2 Answers

It seems that the problem with logging in with seeded users is associated with MVC inaccuracy, look at:

var result = await SignInManager.PasswordSignInAsync(model.Emali, model.Password, model.RememberMe, shouldLockout: false);

if we seed this user:

var user = new ApplicationUser { UserName = "SomeName", Email = "[email protected]" };

result will be == false, but if we change model.Emali in result to model.UserName then login end with success - of course now when we log in, we must give the UserName e.g:

UserName: SomeName;

Passwort: Password5%;

like image 161
Karol Korol Avatar answered Oct 02 '22 21:10

Karol Korol


This worked flawlessly for me.

protected override void Seed(www.Models.ApplicationDbContext context)
        {
            var userStore = new UserStore<ApplicationUser>(context);
            var userManager = new UserManager<ApplicationUser>(userStore);

            if (!context.Users.Any(x => x.UserName == "[email protected]"))
            {
                var user = new ApplicationUser { UserName = "[email protected]", Email = "[email protected]" };
                userManager.Create(user, "Password5%");
                context.Roles.AddOrUpdate(x => x.Name, new IdentityRole { Name = "admin" });
                context.SaveChanges();
                userManager.AddToRole(user.Id, "admin");
            }
        }
like image 41
john Avatar answered Oct 02 '22 22:10

john