Cloudey.Reflex.Authorization 2.0.0

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

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

Reflex

Authorization


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

Features

  • Plug-and-play, no boilerplate required
  • Provides authorization services and providers for use with Autofac DI
  • Define authorization policies without boilerplate
  • 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 Autofac:

using Autofac;

public class MyModule : Module
{
    protected override void Load (ContainerBuilder builder)
    {
        builder.RegisterReflexAuthorization(ThisAssembly); // Provide a list of assemblies to scan for policies and authorization handlers
    }
}

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 { get; } = 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()
{
    // ...
}

Alternatively, you can reference the policy by its name:

using Microsoft.AspNetCore.Authorization; // <-- In this case, using the default ASP.NET Core Authorize attribute also works

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

That's it! There is no need to register the policy anywhere else or define additional handlers.

Assertions

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

public class UserPolicy : IPolicy
{
	public static AuthorizationPolicy Policy { get; } = 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 © 2023 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 138 6/15/2024
3.0.0 149 6/15/2024
2.0.0 144 4/19/2024
1.0.1 208 7/29/2023
1.0.0 380 4/21/2023