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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Avenged.Extensions.DependencyInjection.ServiceAuthorization" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Avenged.Extensions.DependencyInjection.ServiceAuthorization" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Avenged.Extensions.DependencyInjection.ServiceAuthorization" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Avenged.Extensions.DependencyInjection.ServiceAuthorization --version 1.0.0
                    
#r "nuget: Avenged.Extensions.DependencyInjection.ServiceAuthorization, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Avenged.Extensions.DependencyInjection.ServiceAuthorization&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Avenged.Extensions.DependencyInjection.ServiceAuthorization&version=1.0.0
                    
Install as a Cake Tool

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 calls IAuthorizationService 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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