Create Role-Based Web API with ASP.NET Core | Infragistics Blog
Summary
public class AuthResponsesOperationFilter: IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true) .Union(context.MethodInfo.GetCustomAttributes(true)) .OfType(); if (authAttributes.Any()) { var securityRequirement = new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new List() } }; operation.Security = new List { securityRequirement }; operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" }); } } } After that, make sure you have added the filter as an option in Program.cs .AddSwaggerGen method. This model will only require us to provide the essential properties for userEvents, including the title, category, date, userId, and eventId. Creating a role-based API with ASP .NET Core is a crucial aspect when it comes to building secure and scalable web applications. This ensures that only authorized users can access sensitive data or perform critical actions, making your application more secure and reliable. Stay tuned for Part 2 of this tutorial where we are going to demonstrate how to connect that API with App BuilderTM and create a front-end application for it.