Advertisement
Guest User

Untitled

a guest
Sep 10th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. public static void EnsureRolesAreCreated(this IApplicationBuilder app) {
  2. Dictionary<string, string> roles = new Dictionary<string, string>
  3. {
  4. { "Administrator", "Global Access." },
  5. { "User", "Restricted to business domain activity." }
  6. };
  7.  
  8. var context = app.ApplicationServices.GetService<ApplicationDbContext>();
  9. if (context.AllMigrationsApplied()) {
  10. var roleManager = app.ApplicationServices.GetService<ApplicationRoleManager>();
  11. foreach (var role in roles) {
  12. if (!roleManager.RoleExistsAsync(role.Key).Result) {
  13. roleManager.CreateAsync(new ApplicationRole() { Name = role.Key, System = true, Description = role.Value });
  14. }
  15. }
  16. }
  17. }
  18.  
  19. services.AddIdentity<ApplicationUser, ApplicationRole>(config => {
  20. config.User.RequireUniqueEmail = true;
  21. config.Lockout = new LockoutOptions {
  22. AllowedForNewUsers = true,
  23. DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30),
  24. MaxFailedAccessAttempts = 5
  25. };
  26. config.Password = new PasswordOptions {
  27. RequireDigit = true,
  28. RequireNonAlphanumeric = false,
  29. RequireUppercase = true,
  30. RequireLowercase = true,
  31. RequiredLength = 12,
  32. };
  33. })
  34. .AddEntityFrameworkStores<ApplicationDbContext, int>()
  35. .AddUserValidator<ApplicationUserValidator<ApplicationUser>>()
  36. .AddUserManager<ApplicationUserManager>()
  37. .AddRoleManager<ApplicationRoleManager>()
  38. .AddDefaultTokenProviders();
  39.  
  40.  
  41.  
  42. public class ApplicationUser : IdentityUser<int> {}
  43. public class ApplicationRole : IdentityRole<int> {}
  44.  
  45.  
  46. protected override void OnModelCreating(ModelBuilder modelBuilder) {
  47.  
  48. modelBuilder.Entity<ApplicationUser>(i => {
  49. i.HasKey(x => x.Id);
  50. i.Property(x => x.Id).ValueGeneratedOnAdd();
  51. });
  52. modelBuilder.Entity<ApplicationRole>(i => {
  53. i.HasKey(x => x.Id);
  54. i.Property(x => x.Id).ValueGeneratedOnAdd();
  55. });
  56. modelBuilder.Entity<IdentityUserRole<int>>(i => {
  57. i.HasKey(x => new { x.RoleId, x.UserId });
  58. });
  59.  
  60. modelBuilder.Entity<IdentityUserLogin<int>>(i => {
  61. i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
  62. });
  63.  
  64. modelBuilder.Entity<IdentityRoleClaim<int>>(i => {
  65. i.HasKey(x => x.Id);
  66. i.Property(x => x.Id).ValueGeneratedOnAdd();
  67. });
  68.  
  69. modelBuilder.Entity<IdentityUserClaim<int>>(i => {
  70. i.HasKey(x => x.Id);
  71. i.Property(x => x.Id).ValueGeneratedOnAdd();
  72. });
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement