Open Closed

Duplicate api resources in identity server #3407


User avatar
0
nicolas.muniere@soprasteria.com created

We are facing "Duplicate api resources" exception using identity server pro module. This bug has already been reported here a few month ago : https://support.abp.io/QA/Questions/2672/Duplicate-api-resources-in-identity-server

liangshiwei said:

I create an internal issue and will fix it in the next version.

But still no fix available, and the ticket is locked. Can you help?


4 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I have tested, and it works for me,

    Here are my steps:

    Scope

    Api resource

    Client

    Postman

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    After the deep check, it's really a problem, only working the first time.

    You can try this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using IdentityServer4.Configuration;
    using IdentityServer4.Models;
    using IdentityServer4.Stores;
    using Microsoft.Extensions.Caching.Distributed;
    using Microsoft.Extensions.Options;
    using Volo.Abp.Caching;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.IdentityServer;
    using Volo.Abp.IdentityServer.ApiResources;
    using Volo.Abp.IdentityServer.ApiScopes;
    using Volo.Abp.IdentityServer.IdentityResources;
    using Volo.Abp.ObjectMapping;
    using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource;
    using ApiScope = Volo.Abp.IdentityServer.ApiScopes.ApiScope;
    using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource;
    
    namespace Qa.Data;
    
    [ExposeServices(typeof(ResourceStore), typeof(IResourceStore))]
    public class MyResourceStore : ResourceStore
    {
        public MyResourceStore(IIdentityResourceRepository identityResourceRepository,
            IObjectMapper<AbpIdentityServerDomainModule> objectMapper, IApiResourceRepository apiResourceRepository,
            IApiScopeRepository apiScopeRepository,
            IDistributedCache<IdentityServer4.Models.IdentityResource> identityResourceCache,
            IDistributedCache<IdentityServer4.Models.ApiScope> apiScopeCache,
            IDistributedCache<IdentityServer4.Models.ApiResource> apiResourceCache,
            IDistributedCache<Resources> resourcesCache, IOptions<IdentityServerOptions> options) : base(
            identityResourceRepository, objectMapper, apiResourceRepository, apiScopeRepository, identityResourceCache,
            apiScopeCache, apiResourceCache, resourcesCache, options)
        {
        }
    
        protected override async Task<IEnumerable<TModel>> GetCacheItemsAsync<TEntity, TModel>(
            IDistributedCache<TModel> cache, IEnumerable<string> keys, Func<string[], Task<List<TEntity>>> entityFactory,
            Func<List<TModel>, string, List<IEnumerable<KeyValuePair<string, TModel>>>> cacheItemsFactory,
            string cacheKeyPrefix = null)
        {
            var result = await base.GetCacheItemsAsync(cache, keys, entityFactory, cacheItemsFactory,
                    cacheKeyPrefix);
          
            if (result is IEnumerable<IdentityServer4.Models.ApiResource> apiResources)
            {
                return (IEnumerable<TModel>) apiResources.DistinctBy(x => x.Name);
            }
    
            return result;
        }
    }
    

    Add your *.EntityFrameworkCore module

        public override void PreConfigureServices(ServiceConfigurationContext context)
        {
            QaEfCoreEntityExtensionMappings.Configure();
            
            PreConfigure<IIdentityServerBuilder>(builder =>
            {
                builder.AddResourceStore<MyResourceStore>();
            });
            
        }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    We will fix it in the patch version, your ticket has been refunded.

    See: https://github.com/abpframework/abp/issues/13285

  • User Avatar
    0

    Thanks, it's working!

    If this ticket can help someone else, MyResourceStore need to be created in Domain layer

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19