Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Eventures.WebApp.Extentions
- {
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Identity;
- using Models;
- public class SeedRolesMiddleware
- {
- private readonly RequestDelegate _next;
- public SeedRolesMiddleware(RequestDelegate next)
- {
- _next = next;
- }
- public async Task InvokeAsync(HttpContext context,
- UserManager<EventureUser> userManager
- , RoleManager<IdentityRole> roleManager)
- {
- if (!roleManager.Roles.Any())
- {
- await SeedRoles(userManager, roleManager);
- }
- // Call the next delegate/middleware in the pipeline
- await _next(context);
- }
- private async Task SeedRoles(
- UserManager<EventureUser> userManager,
- RoleManager<IdentityRole> roleManager)
- {
- await roleManager.CreateAsync(new IdentityRole
- {
- Name = "admin"
- });
- var user = new EventureUser
- {
- UserName = "AppAdmin",
- FirstName = "Admin",
- LastName = "Admin",
- Email = "[email protected]",
- };
- await userManager.CreateAsync(user, "admin");
- await userManager.AddToRoleAsync(user, "admin");
- }
- }
- }
- namespace Eventures.WebApp.Extentions
- {
- using Microsoft.AspNetCore.Builder;
- public static class SeedRolesMiddlewareExtensions
- {
- public static IApplicationBuilder UseSeedRolesMiddleware(
- this IApplicationBuilder builder)
- {
- return builder.UseMiddleware<SeedRolesMiddleware>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment