Using the template built into VS2017 for Core 2 MVC with Authentication. Trying to seed the database with Users and Roles. Roles work well, they seed. Users always return an error saying "Cannot be NULL", i think its because this line at the end of the initialize function cannot actually find the user
await _userManager.AddToRoleAsync(await _userManager.FindByNameAsync(user), "Administrator");
I used this post to build it: https://gist.github.com/mombrea/9a49716841254ab1d2dabd49144ec092
NOTE - This post is for Core 1.0 and I am using Core 2.0, that might be why I am having problems.
As mentioned, I can seed Roles but the user is not creating itself. Once i figure this out I will try multiple users, i already did a test creating multiple roles.
Here is the Initializer Class
//Taken from :
//https://gist.github.com/mombrea/9a49716841254ab1d2dabd49144ec092
public interface IdentityDBIitializer
{
void Initialize();
}
public class LatestUserDBInitializer : IdentityDBIitializer
{
private readonly ApplicationDbContext _context;
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public LatestUserDBInitializer(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager)
{
_context = context;
_userManager = userManager;
_roleManager = roleManager;
}
//This example just creates an Administrator role and one Admin users
public async void Initialize()
{
//create database schema if none exists
_context.Database.EnsureCreated();
//If there is already an Administrator role, abort
//if (_context.Roles.Any(r => r.Name == "Administrator")) return;
//Create the Administartor Role
//await _roleManager.CreateAsync(new IdentityRole("Administrator"));
//Create the default Admin account and apply the Administrator role
string user = "me@myemail.com";
string password = "z0mgchangethis";
await _userManager.CreateAsync(new ApplicationUser { UserName = user, Email = user, EmailConfirmed = true }, password);
await _userManager.AddToRoleAsync(await _userManager.FindByNameAsync(user), "Administrator");
}
}
Here is my STartup:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<POSContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UKFest2018Context")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UKFestIdentityContext")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add Database Initializer
services.AddScoped<IdentityDBIitializer, LatestUserDBInitializer>();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IdentityDBIitializer dBInitializer)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
//Seeds Identity DB
dBInitializer.Initialize();
//UserRoleInitializer.InitializeUsersAndRoles(app.ApplicationServices);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}