// private BookThruContext db= new BookThruContext(); public HomeController(BookThruContext context, UserManager <BookThruUser> userManager, RoleManager <IdentityRole> roleManager) { _context = context; _userManager = userManager; _roleManager = roleManager; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services, BookThruContext dbContext, RoleManager <IdentityRole> roleManager, UserManager <BookThruUser> userManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseAuthentication(); app.UseSession(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Books}/{action=Index}/{id?}"); }); CreateUserRoles(dbContext, roleManager, userManager).Wait(); }
public IndexModel( UserManager <BookThruUser> userManager, SignInManager <BookThruUser> signInManager, IEmailSender emailSender, BookThruContext context) { _userManager = userManager; _signInManager = signInManager; _emailSender = emailSender; _context = context; }
private async Task CreateUserRoles(BookThruContext dbContext, RoleManager <IdentityRole> RoleManager, UserManager <BookThruUser> UserManager) { dbContext.Database.EnsureCreated(); //initializing custom roles string[] roleNames = { "Admin", "User" }; IdentityResult roleResult; //Create Role if None foreach (var roleName in roleNames) { var roleExist = await RoleManager.RoleExistsAsync(roleName); if (!roleExist) { //create the roles and seed them to the database: Question 1 roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName)); } } //Assign Admin Role To Micheal BookThruUser michaeljaison = await UserManager.FindByEmailAsync("*****@*****.**"); if (michaeljaison == null) { michaeljaison = new BookThruUser() { UserName = "******", Email = "*****@*****.**", }; await UserManager.CreateAsync(michaeljaison, "P@ssw0rd"); } await UserManager.AddToRoleAsync(michaeljaison, "Admin"); // Admin Role To nasir BookThruUser nacer = await UserManager.FindByEmailAsync("*****@*****.**"); if (nacer == null) { nacer = new BookThruUser() { UserName = "******", Email = "*****@*****.**", }; await UserManager.CreateAsync(nacer, "Amin@2005"); } await UserManager.AddToRoleAsync(nacer, "Admin"); }
public BooksController(BookThruContext context, IEmailSender emailSender) { _context = context; _emailSender = emailSender; }