Unable to log in with seed database configuration using asp.net id

I have a login problem, although I can see that the tables are populated with my information about visited users 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 == "admin@admin.com"))
            {
                var userStore = new UserStore<ApplicationUser>(context);
                var userManager = new UserManager<ApplicationUser>(userStore);
                var user = new ApplicationUser { UserName = "admin", Email = "admin@admin.com" };
                var hasher = new PasswordHasher();
                userManager.Create(user, "MySecret5");
                userManager.AddToRole(user.Id, "admin");
            }
        }

and when I try to log in, I get the error "Invalid login attempt". What am I missing?

EDIT: Im in the process of learning all the material about asp.net, so im a pretty big noob now :( so I found that this example works for me, and if anyone else needs it, this:

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 == "admin@v.com"))
            {
                var user = new ApplicationUser { UserName = "admin@v.com", Email = "admin@v.com" };
                userManager.Create(user, "Password5%");
                context.Roles.AddOrUpdate(x => x.Name, new IdentityRole { Name = "admin" });
                context.SaveChanges();
                userManager.AddToRole(user.Id, "admin");
            }
        }

And thanks for your help and time.

+4
source share
2 answers

It 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 == "admin@v.com"))
            {
                var user = new ApplicationUser { UserName = "admin@v.com", Email = "admin@v.com" };
                userManager.Create(user, "Password5%");
                context.Roles.AddOrUpdate(x => x.Name, new IdentityRole { Name = "admin" });
                context.SaveChanges();
                userManager.AddToRole(user.Id, "admin");
            }
        }
+2
source

, MVC, :

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

:

var user = new ApplicationUser { UserName = "SomeName", Email = "admin@v.com" };

== false, model.Emali, model.UserName, - , , , UserName, :

_: SomeName;

: 5%;

+1

Source: https://habr.com/ru/post/1607357/


All Articles