Advertisement
Guest User

Untitled

a guest
May 11th, 2017
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  3. using System.Linq;
  4.  
  5. namespace My.App.Data
  6. {
  7. public class DbInitializer : IDbInitializer
  8. {
  9. private readonly ApplicationDbContext _context;
  10. private readonly UserManager<ApplicationUser> _userManager;
  11. private readonly RoleManager<IdentityRole> _roleManager;
  12.  
  13. public DbInitializer(
  14. ApplicationDbContext context,
  15. UserManager<ApplicationUser> userManager,
  16. RoleManager<IdentityRole> roleManager)
  17. {
  18. _context = context;
  19. _userManager = userManager;
  20. _roleManager = roleManager;
  21. }
  22.  
  23. //This example just creates an Administrator role and one Admin users
  24. public async void Initialize()
  25. {
  26. //create database schema if none exists
  27. _context.Database.EnsureCreated();
  28.  
  29. //If there is already an Administrator role, abort
  30. if (_context.Roles.Any(r => r.Name == "Administrator")) return;
  31.  
  32. //Create the Administartor Role
  33. await _roleManager.CreateAsync(new IdentityRole("Administrator"));
  34.  
  35. //Create the default Admin account and apply the Administrator role
  36. string user = "me@myemail.com";
  37. string password = "z0mgchangethis";
  38. await _userManager.CreateAsync(new ApplicationUser { UserName = user, Email = user, EmailConfirmed = true}, password);
  39. await _userManager.AddToRoleAsync(await _userManager.FindByNameAsync(user), "Administrator");
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement