Skip to content

Commit a8d1ed9

Browse files
committed
Update the samples to use Host.CreateApplicationBuilder() instead of HostBuilder
1 parent 718a23d commit a8d1ed9

File tree

43 files changed

+1046
-1460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1046
-1460
lines changed

samples/Aridka/Aridka.Server/Program.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using Aridka.Server;
2-
using Aridka.Server.Models;
1+
using Aridka.Server.Models;
32
using Microsoft.EntityFrameworkCore;
3+
using OpenIddict.Abstractions;
44
using Quartz;
5+
using static OpenIddict.Abstractions.OpenIddictConstants;
56

67
var builder = WebApplication.CreateBuilder(args);
78

@@ -71,10 +72,6 @@
7172
options.UseAspNetCore();
7273
});
7374

74-
// Register the worker responsible for seeding the database.
75-
// Note: in a real world application, this step should be part of a setup script.
76-
builder.Services.AddHostedService<Worker>();
77-
7875
var app = builder.Build();
7976

8077
app.UseDeveloperExceptionPage();
@@ -89,4 +86,30 @@
8986

9087
app.UseWelcomePage("/");
9188

92-
app.Run();
89+
// Before starting the host, create the database used to store the application data.
90+
//
91+
// Note: in a real world application, this step should be part of a setup script.
92+
await using (var scope = app.Services.CreateAsyncScope())
93+
{
94+
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
95+
await context.Database.EnsureCreatedAsync();
96+
97+
var manager = scope.ServiceProvider.GetRequiredService<IOpenIddictApplicationManager>();
98+
99+
if (await manager.FindByClientIdAsync("console") == null)
100+
{
101+
await manager.CreateAsync(new OpenIddictApplicationDescriptor
102+
{
103+
ClientId = "console",
104+
ClientSecret = "388D45FA-B36B-4988-BA59-B187D329C207",
105+
DisplayName = "My client application",
106+
Permissions =
107+
{
108+
Permissions.Endpoints.Token,
109+
Permissions.GrantTypes.ClientCredentials
110+
}
111+
});
112+
}
113+
}
114+
115+
await app.RunAsync();

samples/Aridka/Aridka.Server/Worker.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

samples/Balosar/Balosar.Server/Program.cs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Balosar.Server.Models;
44
using Microsoft.AspNetCore.Identity;
55
using Microsoft.EntityFrameworkCore;
6+
using OpenIddict.Abstractions;
67
using Quartz;
78
using static OpenIddict.Abstractions.OpenIddictConstants;
89

@@ -129,10 +130,6 @@
129130
builder.Services.AddControllersWithViews();
130131
builder.Services.AddRazorPages();
131132

132-
// Register the worker responsible for seeding the database.
133-
// Note: in a real world application, this step should be part of a setup script.
134-
builder.Services.AddHostedService<Worker>();
135-
136133
var app = builder.Build();
137134

138135
if (builder.Environment.IsDevelopment())
@@ -159,4 +156,50 @@
159156
app.MapControllers();
160157
app.MapFallbackToFile("index.html");
161158

162-
app.Run();
159+
// Before starting the host, create the database used to store the application data.
160+
//
161+
// Note: in a real world application, this step should be part of a setup script.
162+
await using (var scope = app.Services.CreateAsyncScope())
163+
{
164+
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
165+
await context.Database.EnsureCreatedAsync();
166+
167+
var manager = scope.ServiceProvider.GetRequiredService<IOpenIddictApplicationManager>();
168+
169+
if (await manager.FindByClientIdAsync("balosar-blazor-client") is null)
170+
{
171+
await manager.CreateAsync(new OpenIddictApplicationDescriptor
172+
{
173+
ClientId = "balosar-blazor-client",
174+
ConsentType = ConsentTypes.Explicit,
175+
DisplayName = "Blazor client application",
176+
ClientType = ClientTypes.Public,
177+
PostLogoutRedirectUris =
178+
{
179+
new Uri("https://localhost:44310/authentication/logout-callback")
180+
},
181+
RedirectUris =
182+
{
183+
new Uri("https://localhost:44310/authentication/login-callback")
184+
},
185+
Permissions =
186+
{
187+
Permissions.Endpoints.Authorization,
188+
Permissions.Endpoints.EndSession,
189+
Permissions.Endpoints.Token,
190+
Permissions.GrantTypes.AuthorizationCode,
191+
Permissions.GrantTypes.RefreshToken,
192+
Permissions.ResponseTypes.Code,
193+
Permissions.Scopes.Email,
194+
Permissions.Scopes.Profile,
195+
Permissions.Scopes.Roles
196+
},
197+
Requirements =
198+
{
199+
Requirements.Features.ProofKeyForCodeExchange
200+
}
201+
});
202+
}
203+
}
204+
205+
await app.RunAsync();

samples/Balosar/Balosar.Server/Worker.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

samples/Contruum/Contruum.Server/Program.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@
146146
options.EnableAuthorizationEntryValidation();
147147
});
148148

149-
// Register the worker responsible for creating and seeding the SQL database.
150-
// Note: in a real world application, this step should be part of a setup script.
151-
builder.Services.AddHostedService<Worker>();
152-
153149
var app = builder.Build();
154150

155151
if (builder.Environment.IsDevelopment())
@@ -167,4 +163,33 @@
167163

168164
app.MapRazorPages();
169165

170-
app.Run();
166+
// Before starting the host, create the database used to store the application data.
167+
//
168+
// Note: in a real world application, this step should be part of a setup script.
169+
await using (var scope = app.Services.CreateAsyncScope())
170+
{
171+
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
172+
await context.Database.EnsureCreatedAsync();
173+
174+
var manager = scope.ServiceProvider.GetRequiredService<IOpenIddictApplicationManager>();
175+
176+
// Retrieve the client definitions from the configuration
177+
// and insert them in the applications table if necessary.
178+
var descriptors = app.Configuration.GetSection("OpenIddict:Clients").Get<OpenIddictApplicationDescriptor[]>();
179+
if (descriptors is not { Length: > 0 })
180+
{
181+
throw new InvalidOperationException("No client application was found in the configuration file.");
182+
}
183+
184+
foreach (var descriptor in descriptors)
185+
{
186+
if (await manager.FindByClientIdAsync(descriptor.ClientId!) is not null)
187+
{
188+
continue;
189+
}
190+
191+
await manager.CreateAsync(descriptor);
192+
}
193+
}
194+
195+
await app.RunAsync();

samples/Contruum/Contruum.Server/Worker.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)