|
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.AspNetCore.Builder; |
| 3 | +using Microsoft.AspNetCore.Hosting; |
| 4 | +using Microsoft.AspNetCore.Identity; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Hosting; |
| 8 | + |
| 9 | +namespace WebApp |
| 10 | +{ |
| 11 | + public class Startup |
| 12 | + { |
| 13 | + public Startup(IConfiguration configuration) |
| 14 | + { |
| 15 | + Configuration = configuration; |
| 16 | + } |
| 17 | + |
| 18 | + public IConfiguration Configuration { get; } |
| 19 | + |
| 20 | + public void ConfigureServices(IServiceCollection services) |
| 21 | + { |
| 22 | + services.AddAuthentication("Cookie") |
| 23 | + .AddCookie("Cookie"); |
| 24 | + |
| 25 | + services.AddAuthorization(config => |
| 26 | + { |
| 27 | + var policyBuilder = new AuthorizationPolicyBuilder(); |
| 28 | + |
| 29 | + config.DefaultPolicy = policyBuilder |
| 30 | + .AddRequirements(new AutoGeneratedClaim()) |
| 31 | + .Build(); |
| 32 | + }); |
| 33 | + |
| 34 | + services.AddScoped<IAuthorizationHandler, AuthHandler>(); |
| 35 | + |
| 36 | + services.AddSingleton<ClaimsService>(); |
| 37 | + |
| 38 | + services.AddControllersWithViews(); |
| 39 | + } |
| 40 | + |
| 41 | + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) |
| 42 | + { |
| 43 | + if (env.IsDevelopment()) |
| 44 | + { |
| 45 | + app.UseDeveloperExceptionPage(); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + app.UseExceptionHandler("/Error"); |
| 50 | + |
| 51 | + app.UseHsts(); |
| 52 | + } |
| 53 | + |
| 54 | + app.UseHttpsRedirection(); |
| 55 | + app.UseStaticFiles(); |
| 56 | + |
| 57 | + app.UseRouting(); |
| 58 | + |
| 59 | + app.UseAuthentication(); |
| 60 | + app.UseAuthorization(); |
| 61 | + |
| 62 | + app.UseEndpoints(endpoints => |
| 63 | + { |
| 64 | + endpoints.MapDefaultControllerRoute(); |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments