Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenIddict Console Application - Get All Application Clients - ListSync Fails

Tags:

openiddict

I have written a simple console application to get all application clients from OpenIddict server. I tried all the possibilities and getting the syntax error. The code is below. I did not find any example in Github and found some outdated example (2017) is no longer relevant now. Please help

 public static async Task<bool> Test()
 {
            var services = CreateServices();  
            var provider = services.BuildServiceProvider();
            var scope = provider.CreateScope();            
            var context = scope.ServiceProvider.GetRequiredService<CustomDbContext>();
            await context.Database.EnsureCreatedAsync();

            var manager = scope.ServiceProvider.GetRequiredService<IOpenIddictApplicationManager>();

            var result = await manager.FindByClientIdAsync("TestApp");   // It Works
            
            IQueryable<OpenIddictEntityFrameworkCoreApplication> _applicationsQuery = Enumerable.Empty<OpenIddictEntityFrameworkCoreApplication>().AsQueryable();
            _applicationsQuery.Where(apps => apps.ClientId != "");
                                  
            var clients = manager.ListAsync<Func<OpenIddictEntityFrameworkCoreApplication>>(_applicationsQuery);  //Compiler Error

            return (result != null);

}

       

private static IServiceCollection CreateServices()
{
            var services = new ServiceCollection();
            services.AddDbContext<CustomDbContext>(opts =>
            {
                opts.UseSqlServer(
                    ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString,
                    b => b.MigrationsAssembly("Program"));
                opts.UseOpenIddict();
            });

            services.AddOpenIddict() // Register the OpenIddict core components.
                .AddCore(options =>
                {
                    // Configure OpenIddict to use the Entity Framework Core stores and models.
                    // Note: call ReplaceDefaultEntities() to replace the default OpenIddict entities.
                    options.UseEntityFrameworkCore()
                           .UseDbContext<CustomDbContext>();

                    // Enable Quartz.NET integration.
                    options.UseQuartz();
                });

            return services;
}

like image 410
s vinayagam Avatar asked Nov 18 '25 05:11

s vinayagam


1 Answers

ListAsync() returns an IAsyncEnumerable<T> collection, so you can use await foreach to iterate the collection:

await foreach (var application in manager.ListAsync())
{
    Console.WriteLine(await manager.GetClientIdAsync(application));
}

You can also reference the System.Linq.Async package and use the async LINQ extensions. For instance, here's how you could retrieve all the client identifiers of all existing applications:

var identifiers = await manager.ListAsync()
    .SelectAwait(application => manager.GetClientIdAsync(application))
    .ToListAsync();
like image 194
Kévin Chalet Avatar answered Nov 19 '25 17:11

Kévin Chalet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!