|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
4 | 4 | using System.Threading.Tasks;
|
| 5 | +#if (OrganizationalAuth || IndividualB2CAuth) |
| 6 | +using Microsoft.AspNetCore.Authentication; |
| 7 | +using Microsoft.Identity.Web; |
| 8 | +using Microsoft.Identity.Web.UI; |
| 9 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| 10 | +using Microsoft.AspNetCore.Authorization; |
| 11 | +#endif |
| 12 | +using Microsoft.AspNetCore.Builder; |
5 | 13 | using Microsoft.AspNetCore.Hosting;
|
| 14 | +#if (RequiresHttps) |
| 15 | +using Microsoft.AspNetCore.HttpsPolicy; |
| 16 | +#endif |
| 17 | +#if (IndividualLocalAuth) |
| 18 | +using Microsoft.AspNetCore.Identity; |
| 19 | +using Microsoft.AspNetCore.Identity.UI; |
| 20 | +#endif |
| 21 | +#if (OrganizationalAuth) |
| 22 | +using Microsoft.AspNetCore.Mvc.Authorization; |
| 23 | +#endif |
| 24 | +#if (IndividualLocalAuth) |
| 25 | +using Microsoft.EntityFrameworkCore; |
| 26 | +#endif |
6 | 27 | using Microsoft.Extensions.Configuration;
|
| 28 | +using Microsoft.Extensions.DependencyInjection; |
7 | 29 | using Microsoft.Extensions.Hosting;
|
8 | 30 | using Microsoft.Extensions.Logging;
|
| 31 | +#if(MultiOrgAuth) |
| 32 | +using Microsoft.IdentityModel.Tokens; |
| 33 | +#endif |
| 34 | +#if (GenerateGraph) |
| 35 | +using Microsoft.Graph; |
| 36 | +#endif |
| 37 | +#if (IndividualLocalAuth) |
| 38 | +using Company.WebApplication1.Data; |
| 39 | +#endif |
9 | 40 |
|
10 |
| -namespace Company.WebApplication1 |
| 41 | +var builder = WebApplication.CreateBuilder(args); |
| 42 | + |
| 43 | +// Add services to the container. |
| 44 | +#if (IndividualLocalAuth) |
| 45 | +builder.Services.AddDbContext<ApplicationDbContext>(options => |
| 46 | +#if (UseLocalDB) |
| 47 | + options.UseSqlServer( |
| 48 | + builder.Configuration.GetConnectionString("DefaultConnection"))); |
| 49 | +#else |
| 50 | + options.UseSqlite( |
| 51 | + builder.Configuration.GetConnectionString("DefaultConnection"))); |
| 52 | +#endif |
| 53 | +builder.Services.AddDatabaseDeveloperPageExceptionFilter(); |
| 54 | +builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) |
| 55 | + .AddEntityFrameworkStores<ApplicationDbContext>(); |
| 56 | +#elif (OrganizationalAuth) |
| 57 | +#if (GenerateApiOrGraph) |
| 58 | +var initialScopes = builder.Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' '); |
| 59 | + |
| 60 | +#endif |
| 61 | +builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) |
| 62 | +#if (GenerateApiOrGraph) |
| 63 | + .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")) |
| 64 | + .EnableTokenAcquisitionToCallDownstreamApi(initialScopes) |
| 65 | +#if (GenerateApi) |
| 66 | + .AddDownstreamWebApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi")) |
| 67 | +#endif |
| 68 | +#if (GenerateGraph) |
| 69 | + .AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi")) |
| 70 | +#endif |
| 71 | + .AddInMemoryTokenCaches(); |
| 72 | +#else |
| 73 | + .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")); |
| 74 | +#endif |
| 75 | +#elif (IndividualB2CAuth) |
| 76 | +#if (GenerateApi) |
| 77 | +var initialScopes = builder.Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' '); |
| 78 | + |
| 79 | +#endif |
| 80 | +builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) |
| 81 | +#if (GenerateApi) |
| 82 | + .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAdB2C")) |
| 83 | + .EnableTokenAcquisitionToCallDownstreamApi(initialScopes) |
| 84 | + .AddDownstreamWebApi("DownstreamApi", builder.Configuration.GetSection("DownstreamApi")) |
| 85 | + .AddInMemoryTokenCaches(); |
| 86 | +#else |
| 87 | + .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAdB2C")); |
| 88 | +#endif |
| 89 | +#endif |
| 90 | +#if (OrganizationalAuth) |
| 91 | + |
| 92 | +builder.Services.AddAuthorization(options => |
| 93 | +{ |
| 94 | + // By default, all incoming requests will be authorized according to the default policy. |
| 95 | + options.FallbackPolicy = options.DefaultPolicy; |
| 96 | +}); |
| 97 | +builder.Services.AddRazorPages() |
| 98 | + .AddMvcOptions(options => {}) |
| 99 | + .AddMicrosoftIdentityUI(); |
| 100 | +#elif (IndividualB2CAuth) |
| 101 | +builder.Services.AddRazorPages() |
| 102 | + .AddMicrosoftIdentityUI(); |
| 103 | +#else |
| 104 | +builder.Services.AddRazorPages(); |
| 105 | +#endif |
| 106 | + |
| 107 | +var app = builder.Build(); |
| 108 | + |
| 109 | +// Configure the HTTP request pipeline. |
| 110 | +if (app.Environment.IsDevelopment()) |
11 | 111 | {
|
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 |
| - } |
| 112 | + app.UseDeveloperExceptionPage(); |
| 113 | +#if (IndividualLocalAuth) |
| 114 | + app.UseMigrationsEndPoint(); |
| 115 | +#endif |
26 | 116 | }
|
| 117 | +else |
| 118 | +{ |
| 119 | + app.UseExceptionHandler("/Error"); |
| 120 | +#if (RequiresHttps) |
| 121 | + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
| 122 | + app.UseHsts(); |
| 123 | +} |
| 124 | + |
| 125 | +app.UseHttpsRedirection(); |
| 126 | +#else |
| 127 | +} |
| 128 | +#endif |
| 129 | +app.UseStaticFiles(); |
| 130 | + |
| 131 | +#if (OrganizationalAuth || IndividualAuth) |
| 132 | +app.UseAuthentication(); |
| 133 | +#endif |
| 134 | +app.UseAuthorization(); |
| 135 | + |
| 136 | +app.MapRazorPages(); |
| 137 | +#if (IndividualB2CAuth || OrganizationalAuth) |
| 138 | +app.MapControllers(); |
| 139 | +#endif |
| 140 | + |
| 141 | +app.Run(); |
0 commit comments