public virtual void RemoveClaim(Claim claim) { var claimsToRemove = Claims .Where(c => c.Type == claim.Type) .Where(c => c.Value == claim.Value); Claims = Claims.Except(claimsToRemove).ToList(); }
private static async Task SeedUserDatasAsync(IApplicationBuilder app) { using var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope(); var applicationDbContext = serviceScope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); await applicationDbContext.Database.MigrateAsync(); var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >(); var roleManager = serviceScope.ServiceProvider.GetRequiredService <RoleManager <ApplicationRole> >(); foreach ((int UserId, string UserName, string Password, string PhoneNumber, string Email, Guid? TenantId, IEnumerable <Claim> Claims) in SampleDatas.Users()) { ApplicationUser createdUser = await userManager.FindByNameAsync(UserName); if (createdUser is null) { createdUser = new ApplicationUser { UserName = UserName, PhoneNumber = PhoneNumber, Email = Email, TenantId = TenantId }; IdentityResult result = await userManager.CreateAsync(createdUser, Password); var userRoleClaims = Claims.Where(t => t.Type == JwtClaimTypes.Role || t.Type == ClaimTypes.Role); IEnumerable <Claim> userClaims = Claims.Except(userRoleClaims); if (result.Succeeded) { await userManager.AddClaimsAsync(createdUser, userClaims); } var userRoleNames = userRoleClaims?.Select(urc => urc.Value); userRoleNames?.ToList()?.ForEach(userRole => { if (!roleManager.RoleExistsAsync(userRole).Result) { roleManager.CreateAsync(new ApplicationRole { Name = userRole }).Wait(); } }); await userManager.AddToRolesAsync(createdUser, userRoleNames); } } }
public void RemoveClaims(IEnumerable <PersistedClaim> claims) { Claims = Claims.Except(claims); }