예제 #1
0
        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();
        }
예제 #2
0
        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);
                }
            }
        }
예제 #3
0
 public void RemoveClaims(IEnumerable <PersistedClaim> claims)
 {
     Claims = Claims.Except(claims);
 }