WebApi + ClientApp, GraphQL, Reflection

This commit is contained in:
Boris Milašinović
2026-05-06 20:55:05 +02:00
parent 8f7c704a90
commit 4fb3de19f6
196 changed files with 10395 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Events.WebAPI.Util.Swagger;
public static class AddBearerTokenSchemeExtension
{
public static void AddBearerTokenScheme(this SwaggerGenOptions opt)
{
var jwtSecurityScheme = new OpenApiSecurityScheme
{
Description = "Paste token here",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = JwtBearerDefaults.AuthenticationScheme,
BearerFormat = "JWT",
};
//Dodaj Authorize button u Swagger UI
opt.AddSecurityDefinition(jwtSecurityScheme.Scheme, jwtSecurityScheme);
//opt.AddSecurityRequirement(document => new() { [new OpenApiSecuritySchemeReference(jwtSecurityScheme.Scheme, document)] = [] });
// nemoj ga primijeniti na sve operacije (redak iznad), nego samo na one koje imaju Authorize atribut
opt.OperationFilter<AuthorizeOperationFilter>();
}
}

View File

@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Events.WebAPI.Util.Swagger;
public class AuthorizeOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var hasAuthorize = context.MethodInfo.DeclaringType?
.GetCustomAttributes(true)
.OfType<AuthorizeAttribute>().Any() == true
||
context.MethodInfo.GetCustomAttributes(true)
.OfType<AuthorizeAttribute>().Any();
if (!hasAuthorize)
return;
operation.Security ??= new List<OpenApiSecurityRequirement>();
operation.Security.Add(new OpenApiSecurityRequirement
{
[
new OpenApiSecuritySchemeReference(
JwtBearerDefaults.AuthenticationScheme,
context.Document
)
] = new List<string>()
});
}
}