1

I've started in Visual Studio with a new project > ASP NET 5 > Web Application and then followed the initial tutorial here:

https://docs.asp.net/projects/mvc/en/latest/getting-started/first-mvc-app/index.html

One of the tasks I've been struggling with is seeding the database with Users, Roles, and then assigning the Roles to the Users.

This answer, or variations of it look fruitful: https://stackoverflow.com/a/20521530/2591770

But this code:

    var store = new UserStore<ApplicationUser>(context);
    var manager = new UserManager<ApplicationUser>(store);
    var user = new ApplicationUser {UserName = "founder"};

Results in an immediate error.

CS7036 There is no argument given that corresponds to the required formal parameter 'optionsAccessor' of 'UserManager.UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, ILogger>, IHttpContextAccessor)'

Is this a change in the framework or is it likely I've omitted something elsewhere?

I can null the rest of the parameters but can't help feeling that I've missed something.

var userManager = new UserManager<ApplicationUser>(userStore,null,null,null,null,null,null,null,null,null);
Community
  • 1
  • 1
jidl
  • 195
  • 1
  • 2
  • 19
  • When you are logging in with the solution below, make sure username is bob@asd.com or you are loggin in with bob. My problem occured when username and email did not match. – Yablargo May 24 '16 at 14:39

1 Answers1

0

This is how I did it.

public class SeedData
{
    public static void Initialize(IServiceProvider serviceProvider)
    {
        var context = serviceProvider.GetService<ApplicationDbContext>();
        var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();

and then in the Configure method of Startup.cs:

SeedData.Initialize(app.ApplicationServices);

Edit1:

var user0 = new ApplicationUser { UserName = "bob", Email = "bob@asd.com" };
var result = userManager.CreateAsync(user0, "Password1!").Result;
DanielC
  • 472
  • 3
  • 15
  • Everything looks ok in the database, but when I try to login with the created users I get an invalid login attempt. Looking into why, I will post here if I figure it out. – DanielC Feb 22 '16 at 02:21
  • What code are you actually using to create the user? I _think_ they need to async now. – jidl Feb 22 '16 at 11:56