Skip to content

Updated CustomPolicyProvider to DotNet10 Style Project #61474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions src/Security/samples/CustomPolicyProvider/Program.cs
Original file line number Diff line number Diff line change
@@ -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<IAuthorizationPolicyProvider, MinimumAgePolicyProvider>();

// As always, handlers must be provided for the requirements of the authorization policies
builder.Services.AddSingleton<IAuthorizationHandler, MinimumAgeAuthorizationHandler>();

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<Startup>();
}
public partial class Program { }
49 changes: 0 additions & 49 deletions src/Security/samples/CustomPolicyProvider/Startup.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
using CustomPolicyProvider;

namespace AuthSamples.FunctionalTests;

public class CustomPolicyProviderTests : IClassFixture<WebApplicationFactory<CustomPolicyProvider.Startup>>
public class CustomPolicyProviderTests : IClassFixture<WebApplicationFactory<Program>>
{
public CustomPolicyProviderTests(WebApplicationFactory<CustomPolicyProvider.Startup> fixture)
public CustomPolicyProviderTests(WebApplicationFactory<Program> fixture)
{
Client = fixture.CreateClient();
}
Expand Down
Loading