Skip to content

Commit 10f76fa

Browse files
authored
Updated to .NET6 and consolidated Program.cs (#138)
1 parent bc63e22 commit 10f76fa

6 files changed

Lines changed: 119 additions & 137 deletions

File tree

admin/TwoWeeksReady.Admin/Pages/HazardHunts/List.razor

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ else
4343
protected override async Task OnInitializedAsync()
4444
{
4545

46-
_HazardHunts = await Repository.GetAllHazardHunts();
46+
try {
47+
_HazardHunts = await Repository.GetAllHazardHunts();
48+
} catch (NotImplementedException) {
49+
// Application is still growing, let's not throw an error yet
50+
_HazardHunts = Enumerable.Empty<HazardHunt>();
51+
}
4752

4853
}
4954

admin/TwoWeeksReady.Admin/Pages/HazardInfos/List.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ else if (_HazardInfos == null)
3737
else
3838
{
3939
<p>No hazard infos defined</p>
40+
<a class="btn btn-primary" href="/HazardInfos/new">Add New Hazard</a>
4041
}
4142

4243
@code {

admin/TwoWeeksReady.Admin/Pages/Kits/List.razor

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ else
4747
protected override async Task OnInitializedAsync()
4848
{
4949

50-
BaseKits = await Repository.GetAllBaseKits();
50+
try {
51+
BaseKits = await Repository.GetAllBaseKits();
52+
} catch (NotImplementedException)
53+
{
54+
// do nothing for now... site is still growing
55+
BaseKits = Enumerable.Empty<BaseKit>();
56+
}
5157

5258
}
5359

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Hosting;
6-
using Microsoft.Extensions.Configuration;
7-
using Microsoft.Extensions.Hosting;
8-
using Microsoft.Extensions.Logging;
9-
10-
namespace TwoWeeksReady.Admin
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Web;
3+
using TwoWeeksReady.Admin;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Configure the container.
8+
builder.Services.ConfigureServices(builder.Configuration);
9+
10+
var app = builder.Build();
11+
12+
// Configure the HTTP request pipeline.
13+
if (!app.Environment.IsDevelopment())
1114
{
12-
public class Program
13-
{
14-
public static void Main(string[] args)
15-
{
16-
CreateHostBuilder(args).Build().Run();
17-
}
18-
19-
public static IHostBuilder CreateHostBuilder(string[] args) =>
20-
Host.CreateDefaultBuilder(args)
21-
.ConfigureWebHostDefaults(webBuilder =>
22-
{
23-
webBuilder.UseStartup<Startup>();
24-
});
25-
}
15+
app.UseExceptionHandler("/Error");
16+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
17+
app.UseHsts();
2618
}
19+
20+
app.UseHttpsRedirection();
21+
app.UseStaticFiles();
22+
23+
app.UseRouting();
24+
25+
app.UseCookiePolicy();
26+
app.UseAuthentication();
27+
app.UseAuthorization();
28+
29+
app.MapBlazorHub();
30+
app.MapFallbackToPage("/_Host");
31+
32+
app.Run();

admin/TwoWeeksReady.Admin/Startup.cs

Lines changed: 72 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,137 +15,100 @@
1515
using System;
1616
using System.Net.Http;
1717

18-
namespace TwoWeeksReady.Admin
18+
namespace TwoWeeksReady.Admin;
19+
20+
public static class StartupExtensions
1921
{
20-
public class Startup
22+
public static IServiceCollection ConfigureServices(this IServiceCollection services, IConfiguration configuration)
2123
{
22-
public Startup(IConfiguration configuration)
23-
{
24-
Configuration = configuration;
25-
}
26-
27-
public IConfiguration Configuration { get; }
24+
services.AddRazorPages();
25+
services.AddServerSideBlazor();
2826

29-
// This method gets called by the runtime. Use this method to add services to the container.
30-
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
31-
public void ConfigureServices(IServiceCollection services)
27+
services.Configure<CookiePolicyOptions>(options =>
3228
{
33-
services.AddRazorPages();
34-
services.AddServerSideBlazor();
29+
options.CheckConsentNeeded = context => true;
30+
options.MinimumSameSitePolicy = SameSiteMode.None;
31+
});
3532

36-
services.Configure<CookiePolicyOptions>(options =>
37-
{
38-
options.CheckConsentNeeded = context => true;
39-
options.MinimumSameSitePolicy = SameSiteMode.None;
40-
});
33+
// Add authentication services
34+
services.AddAuthentication(options =>
35+
{
36+
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
37+
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
38+
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
39+
})
40+
.AddCookie()
41+
.AddOpenIdConnect("Auth0", options =>
42+
{
43+
options.Authority = $"https://{configuration["Auth0:Domain"]}";
4144

42-
// Add authentication services
43-
services.AddAuthentication(options =>
44-
{
45-
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
46-
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
47-
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
48-
})
49-
.AddCookie()
50-
.AddOpenIdConnect("Auth0", options =>
51-
{
52-
options.Authority = $"https://{Configuration["Auth0:Domain"]}";
45+
options.ClientId = configuration["Auth0:ClientId"];
46+
options.ClientSecret = configuration["Auth0:ClientSecret"];
5347

54-
options.ClientId = Configuration["Auth0:ClientId"];
55-
options.ClientSecret = Configuration["Auth0:ClientSecret"];
48+
options.ResponseType = OpenIdConnectResponseType.Code;
49+
options.SaveTokens = true;
5650

57-
options.ResponseType = OpenIdConnectResponseType.Code;
58-
options.SaveTokens = true;
51+
options.Scope.Clear();
52+
options.Scope.Add("openid");
53+
options.Scope.Add("profile");
5954

60-
options.Scope.Clear();
61-
options.Scope.Add("openid");
62-
options.Scope.Add("profile");
55+
options.CallbackPath = new PathString("/callback");
56+
options.ClaimsIssuer = "Auth0";
6357

64-
options.CallbackPath = new PathString("/callback");
65-
options.ClaimsIssuer = "Auth0";
58+
options.TokenValidationParameters = new TokenValidationParameters
59+
{
60+
NameClaimType = "name",
61+
RoleClaimType = "https://schemas.2wradmin.com/roles"
62+
};
6663

67-
options.TokenValidationParameters = new TokenValidationParameters
64+
options.Events = new OpenIdConnectEvents
65+
{
66+
OnRedirectToIdentityProvider = context =>
6867
{
69-
NameClaimType = "name",
70-
RoleClaimType = "https://schemas.2wradmin.com/roles"
71-
};
72-
73-
options.Events = new OpenIdConnectEvents
68+
// The context's ProtocolMessage can be used to pass along additional query parameters
69+
// to Auth0's /authorize endpoint.
70+
//
71+
// Set the audience query parameter to the API identifier to ensure the returned Access Tokens can be used
72+
// to call protected endpoints on the corresponding API.
73+
context.ProtocolMessage.SetParameter("audience", configuration["Auth0:Audience"]);
74+
75+
return Task.FromResult(0);
76+
},
77+
OnRedirectToIdentityProviderForSignOut = (context) =>
7478
{
75-
OnRedirectToIdentityProvider = context =>
76-
{
77-
// The context's ProtocolMessage can be used to pass along additional query parameters
78-
// to Auth0's /authorize endpoint.
79-
//
80-
// Set the audience query parameter to the API identifier to ensure the returned Access Tokens can be used
81-
// to call protected endpoints on the corresponding API.
82-
context.ProtocolMessage.SetParameter("audience", Configuration["Auth0:Audience"]);
83-
84-
return Task.FromResult(0);
85-
},
86-
OnRedirectToIdentityProviderForSignOut = (context) =>
87-
{
88-
var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";
79+
var logoutUri = $"https://{configuration["Auth0:Domain"]}/v2/logout?client_id={configuration ["Auth0:ClientId"]}";
8980

90-
var postLogoutUri = context.Properties.RedirectUri;
81+
var postLogoutUri = context.Properties.RedirectUri;
9182

92-
if (!string.IsNullOrEmpty(postLogoutUri))
83+
if (!string.IsNullOrEmpty(postLogoutUri))
84+
{
85+
if (postLogoutUri.StartsWith("/"))
9386
{
94-
if (postLogoutUri.StartsWith("/"))
95-
{
96-
var request = context.Request;
97-
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
98-
}
87+
var request = context.Request;
88+
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
9989
}
90+
}
10091

101-
context.Response.Redirect(logoutUri);
102-
context.HandleResponse();
103-
104-
return Task.CompletedTask;
105-
}
106-
};
107-
});
92+
context.Response.Redirect(logoutUri);
93+
context.HandleResponse();
10894

109-
services.AddHttpContextAccessor();
110-
services.AddHttpClient("ApiClient", (HttpClient client) =>
111-
{
112-
client.BaseAddress = new Uri(Configuration["ApiUrl"]);
113-
});
114-
115-
services.AddScoped<TokenProvider>();
116-
//services.AddScoped<IRepository, StubRepository>();
117-
services.AddScoped<IRepository, FunctionsRepository>();
118-
services.AddSingleton<ClientImageService>();
119-
}
95+
return Task.CompletedTask;
96+
}
97+
};
98+
});
12099

121-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
122-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
100+
services.AddHttpContextAccessor();
101+
services.AddHttpClient("ApiClient", (HttpClient client) =>
123102
{
124-
if (env.IsDevelopment())
125-
{
126-
app.UseDeveloperExceptionPage();
127-
}
128-
else
129-
{
130-
app.UseExceptionHandler("/Error");
131-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
132-
app.UseHsts();
133-
}
134-
135-
app.UseHttpsRedirection();
136-
app.UseStaticFiles();
137-
138-
app.UseRouting();
103+
client.BaseAddress = new Uri(configuration["ApiUrl"]);
104+
});
139105

140-
app.UseCookiePolicy();
141-
app.UseAuthentication();
142-
app.UseAuthorization();
106+
services.AddScoped<TokenProvider>();
107+
//services.AddScoped<IRepository, StubRepository>();
108+
services.AddScoped<IRepository, FunctionsRepository>();
109+
services.AddSingleton<ClientImageService>();
143110

144-
app.UseEndpoints(endpoints =>
145-
{
146-
endpoints.MapBlazorHub();
147-
endpoints.MapFallbackToPage("/_Host");
148-
});
149-
}
111+
return services;
150112
}
113+
151114
}

admin/TwoWeeksReady.Admin/TwoWeeksReady.Admin.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<UserSecretsId>4c82d094-3afe-4274-922b-9c0d8bdda7c5</UserSecretsId>
6+
<ImplicitUsings>enable</ImplicitUsings>
67
</PropertyGroup>
78

89
<ItemGroup>
@@ -16,7 +17,7 @@
1617
</ItemGroup>
1718

1819
<ItemGroup>
19-
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.6" />
20+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.2" />
2021
<PackageReference Include="TinyMCE.Blazor" Version="0.0.8" />
2122
</ItemGroup>
2223

0 commit comments

Comments
 (0)