Avenged.Extensions.DependencyInjection.ServiceAuthorization
1.0.0
dotnet add package Avenged.Extensions.DependencyInjection.ServiceAuthorization --version 1.0.0
NuGet\Install-Package Avenged.Extensions.DependencyInjection.ServiceAuthorization -Version 1.0.0
<PackageReference Include="Avenged.Extensions.DependencyInjection.ServiceAuthorization" Version="1.0.0" />
<PackageVersion Include="Avenged.Extensions.DependencyInjection.ServiceAuthorization" Version="1.0.0" />
<PackageReference Include="Avenged.Extensions.DependencyInjection.ServiceAuthorization" />
paket add Avenged.Extensions.DependencyInjection.ServiceAuthorization --version 1.0.0
#r "nuget: Avenged.Extensions.DependencyInjection.ServiceAuthorization, 1.0.0"
#addin nuget:?package=Avenged.Extensions.DependencyInjection.ServiceAuthorization&version=1.0.0
#tool nuget:?package=Avenged.Extensions.DependencyInjection.ServiceAuthorization&version=1.0.0
ServiceAuthorization
A lightweight .NET library that adds automatic role-based authorization to service classes using Castle DynamicProxy and ASP.NET Core's IAuthorizationService
.
Easily protect your service methods by decorating them with [Authorize]
attributes, just like in MVC controllers.
โจ Features
- Intercepts service method calls and checks authorization before execution.
- Supports
[Authorize]
attributes on class and method level. - Fully compatible with ASP.NET Core authorization and role-based policies.
- Simple integration with
IServiceCollection
.
๐ฆ Installation
Install via NuGet:
dotnet add package Avenged.Extensions.DependencyInjection.ServiceAuthorization
โ๏ธ Requirements
- .NET Standard 2.1 or .NET 6.0+
- ASP.NET Core's authorization system must be set up
- Your project must already reference:
<PackageReference Include="Microsoft.AspNetCore.Authorization" />
๐ Getting Started
1. Register authorization services
In your Startup.cs
(for .NET Core 3.1/5) or Program.cs
(for .NET 6/7/8):
services.AddAuthorization(); // if not already added
services.AddHttpContextAccessor();
services.AddServiceAuthorization();
2. Register your service with authorization support
services.AddAuthorizedService<IProductService, ProductService>();
3. Use [Authorize]
on your service
[Authorize(Roles = "Admin")]
public class ProductService : IProductService
{
public void DeleteProduct(int id)
{
// This method requires an Admin role
}
[Authorize(Roles = "Manager")]
public void UpdateProduct(int id)
{
// This method requires a Manager role
}
}
Authorization will be evaluated automatically before method execution. If unauthorized, an
UnauthorizedAccessException
will be thrown.
๐ How It Works
- A dynamic proxy wraps your service implementation.
- The proxy intercepts calls and reads any
[Authorize]
attributes present. - It checks the current user via
HttpContext
and callsIAuthorizationService
to enforce role or policy requirements.
๐งช Example Use Case
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
builder.Services.AddHttpContextAccessor();
builder.Services.AddServiceAuthorization();
builder.Services.AddAuthorizedService<IMyService, MyService>();
var app = builder.Build();
๐ License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Castle.Core (>= 5.0.0)
- Microsoft.AspNetCore.Authorization (>= 6.0.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.1.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 125 | 6/26/2025 |