|
1 |
| -namespace WebAppWithADSample; |
| 1 | +using Microsoft.AspNetCore.Authentication.OpenIdConnect; |
| 2 | +using Microsoft.Identity.Web; |
| 3 | +using Microsoft.Identity.Web.UI; |
2 | 4 |
|
3 |
| -public class Program |
| 5 | +var builder = WebApplication.CreateBuilder(args); |
| 6 | + |
| 7 | +builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) |
| 8 | + .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd")); |
| 9 | + |
| 10 | +builder.Services.AddAuthorization(options => |
4 | 11 | {
|
5 |
| - public static void Main(string[] args) |
| 12 | + options.AddPolicy("Developers", policy => |
6 | 13 | {
|
7 |
| - CreateHostBuilder(args).Build().Run(); |
8 |
| - } |
9 |
| - |
10 |
| - public static IHostBuilder CreateHostBuilder(string[] args) => |
11 |
| - Host.CreateDefaultBuilder(args) |
12 |
| - .ConfigureWebHostDefaults(webBuilder => |
13 |
| - { |
14 |
| - webBuilder.UseStartup<Startup>(); |
15 |
| - }); |
| 14 | + policy.RequireRole("DevGroup"); |
| 15 | + }); |
| 16 | + options.AddPolicy("Employees", policy => |
| 17 | + { |
| 18 | + policy.RequireClaim("EmployeeNumber"); |
| 19 | + }); |
| 20 | + // By default, all incoming requests will be authorized according to the default policy |
| 21 | + options.FallbackPolicy = options.DefaultPolicy; |
| 22 | +}); |
| 23 | + |
| 24 | +builder.Services.AddRazorPages() |
| 25 | + .AddMvcOptions(options => { }) |
| 26 | + .AddMicrosoftIdentityUI(); |
| 27 | + |
| 28 | +var app = builder.Build(); |
| 29 | + |
| 30 | +if (app.Environment.IsDevelopment()) |
| 31 | +{ |
| 32 | + app.UseDeveloperExceptionPage(); |
16 | 33 | }
|
| 34 | +else |
| 35 | +{ |
| 36 | + app.UseExceptionHandler("/Error"); |
| 37 | + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. |
| 38 | + app.UseHsts(); |
| 39 | +} |
| 40 | + |
| 41 | +app.UseHttpsRedirection(); |
| 42 | +app.UseStaticFiles(); |
| 43 | + |
| 44 | +app.UseRouting(); |
| 45 | + |
| 46 | +app.UseAuthentication(); |
| 47 | +app.UseAuthorization(); |
| 48 | + |
| 49 | +app.MapRazorPages(); |
| 50 | +app.MapControllers(); |
| 51 | + |
| 52 | +app.Run(); |
0 commit comments