|
15 | 15 | using System; |
16 | 16 | using System.Net.Http; |
17 | 17 |
|
18 | | -namespace TwoWeeksReady.Admin |
| 18 | +namespace TwoWeeksReady.Admin; |
| 19 | + |
| 20 | +public static class StartupExtensions |
19 | 21 | { |
20 | | - public class Startup |
| 22 | + public static IServiceCollection ConfigureServices(this IServiceCollection services, IConfiguration configuration) |
21 | 23 | { |
22 | | - public Startup(IConfiguration configuration) |
23 | | - { |
24 | | - Configuration = configuration; |
25 | | - } |
26 | | - |
27 | | - public IConfiguration Configuration { get; } |
| 24 | + services.AddRazorPages(); |
| 25 | + services.AddServerSideBlazor(); |
28 | 26 |
|
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 => |
32 | 28 | { |
33 | | - services.AddRazorPages(); |
34 | | - services.AddServerSideBlazor(); |
| 29 | + options.CheckConsentNeeded = context => true; |
| 30 | + options.MinimumSameSitePolicy = SameSiteMode.None; |
| 31 | + }); |
35 | 32 |
|
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"]}"; |
41 | 44 |
|
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"]; |
53 | 47 |
|
54 | | - options.ClientId = Configuration["Auth0:ClientId"]; |
55 | | - options.ClientSecret = Configuration["Auth0:ClientSecret"]; |
| 48 | + options.ResponseType = OpenIdConnectResponseType.Code; |
| 49 | + options.SaveTokens = true; |
56 | 50 |
|
57 | | - options.ResponseType = OpenIdConnectResponseType.Code; |
58 | | - options.SaveTokens = true; |
| 51 | + options.Scope.Clear(); |
| 52 | + options.Scope.Add("openid"); |
| 53 | + options.Scope.Add("profile"); |
59 | 54 |
|
60 | | - options.Scope.Clear(); |
61 | | - options.Scope.Add("openid"); |
62 | | - options.Scope.Add("profile"); |
| 55 | + options.CallbackPath = new PathString("/callback"); |
| 56 | + options.ClaimsIssuer = "Auth0"; |
63 | 57 |
|
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 | + }; |
66 | 63 |
|
67 | | - options.TokenValidationParameters = new TokenValidationParameters |
| 64 | + options.Events = new OpenIdConnectEvents |
| 65 | + { |
| 66 | + OnRedirectToIdentityProvider = context => |
68 | 67 | { |
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) => |
74 | 78 | { |
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"]}"; |
89 | 80 |
|
90 | | - var postLogoutUri = context.Properties.RedirectUri; |
| 81 | + var postLogoutUri = context.Properties.RedirectUri; |
91 | 82 |
|
92 | | - if (!string.IsNullOrEmpty(postLogoutUri)) |
| 83 | + if (!string.IsNullOrEmpty(postLogoutUri)) |
| 84 | + { |
| 85 | + if (postLogoutUri.StartsWith("/")) |
93 | 86 | { |
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; |
99 | 89 | } |
| 90 | + } |
100 | 91 |
|
101 | | - context.Response.Redirect(logoutUri); |
102 | | - context.HandleResponse(); |
103 | | - |
104 | | - return Task.CompletedTask; |
105 | | - } |
106 | | - }; |
107 | | - }); |
| 92 | + context.Response.Redirect(logoutUri); |
| 93 | + context.HandleResponse(); |
108 | 94 |
|
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 | + }); |
120 | 99 |
|
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) => |
123 | 102 | { |
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 | + }); |
139 | 105 |
|
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>(); |
143 | 110 |
|
144 | | - app.UseEndpoints(endpoints => |
145 | | - { |
146 | | - endpoints.MapBlazorHub(); |
147 | | - endpoints.MapFallbackToPage("/_Host"); |
148 | | - }); |
149 | | - } |
| 111 | + return services; |
150 | 112 | } |
| 113 | + |
151 | 114 | } |
0 commit comments