IdentityServer4 with .Net7.0. API separation in SSO by ApiScope

I have 5 clients of my identity server, I need to separate the authorization of 1 client from the others. After signin to the ClientA, I switch to the ClientB already authorized, help me solve.
It is necessary for one IdentityServer to work as two?

my IdentityResourceStore:

public class IdentityResourceStore : IResourceStore {
        private readonly IScopeRepository _repository;
        private readonly IMemoryCache _memoryCache;

        public IdentityResourceStore(IScopeRepository repository, IMemoryCache memoryCache) {
            _repository = repository;
            _memoryCache = memoryCache;
        }

        public async Task<IEnumerable<IdentityResource>> FindIdentityResourcesByScopeNameAsync(
            IEnumerable<string> scopeNames) {
            var resources = await GetAllResourcesAsync();
            return resources.IdentityResources.Where(r => scopeNames.Contains(r.Name));
        }

        public async Task<IEnumerable<ApiScope>> FindApiScopesByNameAsync(IEnumerable<string> scopeNames) {
            var resources = await GetAllResourcesAsync();
            return resources.ApiScopes.Where(s => scopeNames.Contains(s.Name));
        }

        public async Task<IEnumerable<ApiResource>> FindApiResourcesByScopeNameAsync(IEnumerable<string> scopeNames) {
            var resources = await GetAllResourcesAsync();
            var apiResources = resources.ApiResources.ToList();
            return apiResources.Where(x => x.Scopes.Any(y => scopeNames.Contains(y)));
        }

        public async Task<IEnumerable<ApiResource>> FindApiResourcesByNameAsync(IEnumerable<string> apiResourceNames) {
            var resources = await GetAllResourcesAsync();
            var apiResources = resources.ApiResources.ToList();
            return apiResources.Where(x => apiResourceNames.Contains(x.Name));
        }
        
        public async Task<IdentityServer4.Models.Resources> GetAllResourcesAsync() {
            var defaultResources = new List<IdentityResource> {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email()
            };
            ApiScope[] apiScopes = { new ApiScope("ClientA"), new ApiScope("ClientB") };
            var apiResources = new List<ApiResource>();
            if (_memoryCache.TryGetValue("clients_array", out IEnumerable<string> clients_array)) {
                foreach (var clientId in clients_array) {
                    if (!_memoryCache.TryGetValue(clientId, out FileClient client)) continue;

                    if (clientId == ClientIds.ClientB) { 
                        var apiResource = new ApiResource(clientId) {
                            ApiSecrets = client.IdentitySeverClient.ClientSecrets,                        
                            Scopes = new [] { "ClientB" },
                        };
                        apiResources.Add(apiResource);
                    }
                    else
                    {
                        var apiResource = new ApiResource(clientId) {
                            ApiSecrets = client.IdentitySeverClient.ClientSecrets,                        
                            Scopes = new [] { "ClientA" },
                        };
                        apiResources.Add(apiResource);
                    }
                }
            }

            return new IdentityServer4.Models.Resources(defaultResources, apiResources, apiScopes);
        }
    }

One of the auth endpoints in my identityServer

   public async Task<IActionResult> AuthByPhonePassword([FromBody] AuthLoginPasswordDto authUserByPhoneDto) {
            var result = await _authService.AuthByPhonePassword(authUserByPhoneDto);

            await HttpContext.SignInAsync(new IdentityServerUser(result.Value.UserId) {
                DisplayName = result.Value.UserName,
                AdditionalClaims = new Claim[] { new Claim(CustomJwtClaimTypes.UserType, result.Value.UserType) }
            }, null);

            return result.AsHttpResult();
        }

ClientA scopes: “scopes”: “openid profile employee ClientA offline_access”,
ClientB scopes: “scopes”: “openid profile ClientB”,

I attempted to separate different scopes. Tried to add a Policy on the API side, but this approach turned out to be unsuitable. It seems that my IdentityServer must be the one to indicate that a scope is not appropriate.
I noticed that after modifying resources, my JWT token began to change; the ‘aud’ list was correct (without any unnecessary clients), and the list of scopes in the token was also corrected.

New contributor

Галымжан Оралбаев is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật