Cloudey.Reflex.Authorization 3.0.1

Prefix Reserved
dotnet add package Cloudey.Reflex.Authorization --version 3.0.1                
NuGet\Install-Package Cloudey.Reflex.Authorization -Version 3.0.1                
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="Cloudey.Reflex.Authorization" Version="3.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cloudey.Reflex.Authorization --version 3.0.1                
#r "nuget: Cloudey.Reflex.Authorization, 3.0.1"                
#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.
// Install Cloudey.Reflex.Authorization as a Cake Addin
#addin nuget:?package=Cloudey.Reflex.Authorization&version=3.0.1

// Install Cloudey.Reflex.Authorization as a Cake Tool
#tool nuget:?package=Cloudey.Reflex.Authorization&version=3.0.1                

Reflex

Authorization


Plug-and-play authorization extending ASP.NET Core Authorization to make it easier and more pleasant to use.

Features

  • Plug-and-play, no boilerplate required
  • Auto-discover authorization policies with assembly scanning
  • Easily define authorization policies in separate classes
  • Policies are auto-registered with the DI container
  • Reference policies by type instead of name for better type safety

Installation

Install with NuGet

Register the authorization services with the service provider (in Program.cs), providing a list of assemblies to scan for policies:

services.AddReflexAuthorization(typeof(Program).Assembly, typeof(TypeInSomeOtherAssembly).Assembly);

You can also configure a default and/or fallback policy as well as other authorization options:

services.AddReflexAuthorization(
    options => 
        options.FallbackPolicy = new AuthorizationPolicyBuilder.RequireAuthenticatedUser().Build(), 
    typeof(Program).Assembly, typeof(TypeInSomeOtherAssembly).Assembly);

If you are not using ASP.NET Core Authorization already, register the authorization middleware:

// Program.cs

var builder = WebApplication.CreateBuilder(args);
// ...
var app = builder.Build();
// ...
app.UseAuthorization(); // <-- Register authorization middleware
// ...

await app.RunAsync();

Usage

Policies

Define policies anywhere in your application by implementing the IPolicy interface:

public class MustBeAdminPolicy : IPolicy
{
	public static AuthorizationPolicy Policy => new AuthorizationPolicyBuilder()
		.RequireAuthenticatedUser()
		.RequireRole("Admin")
		.Build();
}

Use the policy with the extended Authorize attribute:

using Cloudey.Reflex.Authorization; // <-- Use the Authorize attribute from this library

// ...

[Authorize<MustBeAdminPolicy>] // <-- Use the policy type instead of the policy name
[HttpGet]
public void AdminOnlyEndpoint()
{
    // ...
}

That's it! The policy will be auto-discovered from the assembly and applied to the endpoint.

Assertions

Assertions are easy ways to add more complex requirements to your policies.

public class UserPolicy : IPolicy
{
	public static AuthorizationPolicy Policy => new AuthorizationPolicyBuilder()
		.RequireAuthenticatedUser()
		.RequireAssertion(context => context.User.HasClaim("superuser", "true"))
		.Build();
}

There is no further configuration required to handle assertion requirements.

GraphQL / HotChocolate

If you are using HotChocolate for GraphQL, check out Cloudey.Reflex.Authorization.HotChocolate for a plug-and-play solution to authorization in GraphQL.

License

Licensed under Apache 2.0.
Copyright © 2024 Cloudey IT Ltd
Cloudey® is a registered trademark of Cloudey IT Ltd. Use of the trademark is NOT GRANTED under the license of this repository or software package.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Cloudey.Reflex.Authorization:

Package Downloads
Cloudey.Reflex.Authorization.HotChocolate

Utilities for easy-to-use authorization with HotChocolate GraphQL server. - Assertions for types, fields, and parent types - Easy policy-based authorization - Works with Cloudey.Reflex.Authorization See README for usage instructions.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.1 134 6/15/2024
3.0.0 145 6/15/2024
2.0.0 142 4/19/2024
1.0.1 206 7/29/2023
1.0.0 378 4/21/2023