Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
- using System.Linq;
- namespace My.App.Data
- {
- public class DbInitializer : IDbInitializer
- {
- private readonly ApplicationDbContext _context;
- private readonly UserManager<ApplicationUser> _userManager;
- private readonly RoleManager<IdentityRole> _roleManager;
- public DbInitializer(
- 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");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement