Skip to content

Commit c9e62f7

Browse files
committed
.
1 parent 5235345 commit c9e62f7

7 files changed

+86
-46
lines changed

ApiPerfComparison.sln

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiPerfComparison", "ApiPerfComparison\ApiPerfComparison.csproj", "{A35F5655-9910-4218-B2E9-8DE72A782CFB}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkHelper", "BenchmarkHelper\BenchmarkHelper.csproj", "{62A1FB3B-0CDB-4C59-B0E3-947B6C80DAFA}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject1", "TestProject1\TestProject1.csproj", "{1F04BB61-1B42-4934-A8A7-184E897052B0}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{A35F5655-9910-4218-B2E9-8DE72A782CFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{A35F5655-9910-4218-B2E9-8DE72A782CFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{A35F5655-9910-4218-B2E9-8DE72A782CFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{A35F5655-9910-4218-B2E9-8DE72A782CFB}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{62A1FB3B-0CDB-4C59-B0E3-947B6C80DAFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{62A1FB3B-0CDB-4C59-B0E3-947B6C80DAFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{62A1FB3B-0CDB-4C59-B0E3-947B6C80DAFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{62A1FB3B-0CDB-4C59-B0E3-947B6C80DAFA}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{1F04BB61-1B42-4934-A8A7-184E897052B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{1F04BB61-1B42-4934-A8A7-184E897052B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{1F04BB61-1B42-4934-A8A7-184E897052B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{1F04BB61-1B42-4934-A8A7-184E897052B0}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
EndGlobal

ApiPerfComparison/Auth/ApiKeyConstants.cs

-38
This file was deleted.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.AspNetCore.Authentication.JwtBearer;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.IdentityModel.Tokens;
4+
5+
using System.Text;
6+
7+
namespace ApiPerfComparison.Auth;
8+
9+
public static class AuthExtensions
10+
{
11+
public static void AddApiKeySupport(this IServiceCollection services, IConfiguration configuration)
12+
{
13+
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
14+
{
15+
options.TokenValidationParameters = new TokenValidationParameters()
16+
{
17+
ValidateActor = true,
18+
ValidateAudience = true,
19+
ValidateLifetime = true,
20+
ValidateIssuerSigningKey = true,
21+
ValidIssuer = configuration["Jwt:Issuer"],
22+
ValidAudience = configuration["Jwt:Audience"],
23+
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"]))
24+
};
25+
});
26+
27+
//services.AddAuthorizationBuilder()
28+
// .SetFallbackPolicy(new AuthorizationPolicyBuilder()
29+
// .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
30+
// .RequireAuthenticatedUser()
31+
// .Build());
32+
33+
services.AddAuthorization();
34+
}
35+
36+
}

ApiPerfComparison/MinimalApi/EndpointRouteBuilderExtensions.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ public static void RegisterEndpoints(this IEndpointRouteBuilder app)
7171
}
7272

7373
return Results.Unauthorized();
74-
});
74+
}).AllowAnonymous();
7575

76-
app.MapGet("/secure", [Authorize] () => "This is a secured endpoint!")
76+
app.MapGet("/secure", () => "This is a secured endpoint!")
7777
.RequireAuthorization();
7878

79-
app.MapGet("/secure1", () => "This is a secured endpoint!");
79+
app.MapGet("/notSecure1", () => "This is a secured endpoint!")
80+
.AllowAnonymous();
8081

8182

8283
app.MapPost("minimalapi/createV1", ([FromBody] UserDto user, IValidator<UserDto> validator) =>

ApiPerfComparison/Program.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88

99
builder.Services.AddControllers();
1010
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
11-
builder.Services.AddAuthorization();
12-
builder.Services.AddApiKeySupport(new ApiKeyConstants());
11+
12+
builder.Services.AddApiKeySupport(builder.Configuration);
13+
1314

1415
var app = builder.Build();
1516

17+
app.UseAuthorization();
18+
app.UseAuthentication();
19+
1620
app.MapControllers();
1721
app.RegisterEndpoints();
1822

1923
// Register the middleware
2024
//app.UseMiddleware<ValidationMiddleware>();
2125

22-
app.UseAuthentication();
23-
app.UseAuthorization();
2426

2527
app.Run();
2628

ApiPerfComparison/appsettings.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"AllowedHosts": "*"
8+
"AllowedHosts": "*",
9+
"Jwt": {
10+
"Key": "your-secure-key-here-1234567890_xxsfa4564@##%^&&",
11+
"Issuer": "https://localhost:7000/",
12+
"Audience": "https://localhost:7000/"
13+
}
914
}

0 commit comments

Comments
 (0)