From 2c85517713dd518ab752a201efd2c4745cb107cc Mon Sep 17 00:00:00 2001 From: samabos Date: Sun, 13 Apr 2025 15:35:10 +0100 Subject: [PATCH] updated the program.cs to top-level program style, removed startup.cs and made changes to make the tests to detect the program.cs. --- .../samples/CustomPolicyProvider/Program.cs | 51 ++++++++++++++----- .../samples/CustomPolicyProvider/Startup.cs | 49 ------------------ .../CustomPolicyProviderTests.cs | 5 +- 3 files changed, 41 insertions(+), 64 deletions(-) delete mode 100644 src/Security/samples/CustomPolicyProvider/Startup.cs diff --git a/src/Security/samples/CustomPolicyProvider/Program.cs b/src/Security/samples/CustomPolicyProvider/Program.cs index 10f33513900e..18a263393206 100644 --- a/src/Security/samples/CustomPolicyProvider/Program.cs +++ b/src/Security/samples/CustomPolicyProvider/Program.cs @@ -1,19 +1,44 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authorization; -namespace CustomPolicyProvider; +using CustomPolicyProvider; -public class Program -{ - public static void Main(string[] args) +var builder = WebApplication.CreateBuilder(args); + +// Replace the default authorization policy provider with our own +// custom provider which can return authorization policies for given +// policy names (instead of using the default policy provider) +builder.Services.AddSingleton(); + +// As always, handlers must be provided for the requirements of the authorization policies +builder.Services.AddSingleton(); + +builder.Services.AddControllersWithViews(); + +// Add cookie authentication so that it's possible to sign-in to test the +// custom authorization policy behavior of the sample +builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie(options => { - CreateWebHostBuilder(args).Build().Run(); - } + options.AccessDeniedPath = "/account/denied"; + options.LoginPath = "/account/signin"; + }); + +var app = builder.Build(); + +app.UseRouting(); + +app.UseAuthentication(); +app.UseAuthorization(); + +app.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}") + .WithStaticAssets(); + +app.Run(); - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); -} +public partial class Program { } diff --git a/src/Security/samples/CustomPolicyProvider/Startup.cs b/src/Security/samples/CustomPolicyProvider/Startup.cs deleted file mode 100644 index f1c0caf8d54b..000000000000 --- a/src/Security/samples/CustomPolicyProvider/Startup.cs +++ /dev/null @@ -1,49 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace CustomPolicyProvider; - -public class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - // Replace the default authorization policy provider with our own - // custom provider which can return authorization policies for given - // policy names (instead of using the default policy provider) - services.AddSingleton(); - - // As always, handlers must be provided for the requirements of the authorization policies - services.AddSingleton(); - - services.AddMvc(); - - // Add cookie authentication so that it's possible to sign-in to test the - // custom authorization policy behavior of the sample - services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(options => - { - options.AccessDeniedPath = "/account/denied"; - options.LoginPath = "/account/signin"; - }); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.UseRouting(); - - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapDefaultControllerRoute(); - }); - } -} diff --git a/src/Security/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs b/src/Security/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs index 975d37b0ac9a..f25b362ebf1e 100644 --- a/src/Security/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs +++ b/src/Security/test/AuthSamples.FunctionalTests/CustomPolicyProviderTests.cs @@ -9,12 +9,13 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Testing; using Xunit; +using CustomPolicyProvider; namespace AuthSamples.FunctionalTests; -public class CustomPolicyProviderTests : IClassFixture> +public class CustomPolicyProviderTests : IClassFixture> { - public CustomPolicyProviderTests(WebApplicationFactory fixture) + public CustomPolicyProviderTests(WebApplicationFactory fixture) { Client = fixture.CreateClient(); }