mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-19 02:12:10 +08:00
33 lines
1.2 KiB
C#
33 lines
1.2 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace BuildingBlocks.TestBase.Auth;
|
|
|
|
//ref: https://blog.joaograssi.com/posts/2021/asp-net-core-testing-permission-protected-api-endpoints/
|
|
public static class AuthServiceCollectionExtensions
|
|
{
|
|
private static MockAuthUser GetMockUser() =>
|
|
new MockAuthUser(new Claim("sub", Guid.NewGuid().ToString()),
|
|
new Claim("email", "sam@test.com"));
|
|
|
|
public static AuthenticationBuilder AddTestAuthentication(this IServiceCollection services)
|
|
{
|
|
services.AddAuthorization(options =>
|
|
{
|
|
// AuthConstants.Scheme is just a scheme we define. I called it "TestAuth"
|
|
options.DefaultPolicy = new AuthorizationPolicyBuilder("Test")
|
|
.RequireAuthenticatedUser()
|
|
.Build();
|
|
});
|
|
|
|
// Register a default user, so all requests have it by default
|
|
services.AddScoped(_ => GetMockUser());
|
|
|
|
// Register our custom authentication handler
|
|
return services.AddAuthentication("Test")
|
|
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { });
|
|
}
|
|
}
|