devdeer.Libraries.Repository.EntityFrameworkCore.Authorized 13.0.0

dotnet add package devdeer.Libraries.Repository.EntityFrameworkCore.Authorized --version 13.0.0
                    
NuGet\Install-Package devdeer.Libraries.Repository.EntityFrameworkCore.Authorized -Version 13.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="devdeer.Libraries.Repository.EntityFrameworkCore.Authorized" Version="13.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="devdeer.Libraries.Repository.EntityFrameworkCore.Authorized" Version="13.0.0" />
                    
Directory.Packages.props
<PackageReference Include="devdeer.Libraries.Repository.EntityFrameworkCore.Authorized" />
                    
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 devdeer.Libraries.Repository.EntityFrameworkCore.Authorized --version 13.0.0
                    
#r "nuget: devdeer.Libraries.Repository.EntityFrameworkCore.Authorized, 13.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.
#:package devdeer.Libraries.Repository.EntityFrameworkCore.Authorized@13.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=devdeer.Libraries.Repository.EntityFrameworkCore.Authorized&version=13.0.0
                    
Install as a Cake Addin
#tool nuget:?package=devdeer.Libraries.Repository.EntityFrameworkCore.Authorized&version=13.0.0
                    
Install as a Cake Tool

devdeer.Libraries.Repository.EntityFrameworkCore.Authorized

NuGet Downloads

Disclaimer

If you want to use this package you should be aware of some principles and practices we at DEVDEER use. So be aware that this is not backed by a public repository. The purpose here is to give out customers easy access to our logic. Nevertheless you are welcome to use this lib if it provides any advantages.

Summary

The purpose of this package is to simplify and standardize the process of performing HTTP context user detection and automated role management for an API using Azure AD and a backend using EF Core code first.

Using this package automatically leads to changes in the database structure. Again be aware that this is completely built around the standards of DEVDEER.

Usage

After you added a Nuget reference to your project you should first inherit your DbContext from SimpleAuthorizationContext instead of DbContext. This will add some additional logic to your context which is needed to work with the repository.

public class MyContext : SimpleAuthorizationContext
{
     public MyContext(DbContextOptions dbContextOptions,
        IOptions<EntityFrameworkAuthorizationOptions> authorizationOptions) : base(dbContextOptions, authorizationOptions)
     {
     }

}

You should now take care about your settings. In the appsettings.json there should be a new setting:

"EfCoreAuthorization": {
    "IsEnabled": true,
    "AutoCreateNewUsers": true,
    "AutoCreateRoleKey": "GUE",
    "UseCaching": true,
    "CacheTimeoutSeconds": 1800,
    "RequiredRoles": [
        {
            "Id": 1,
            "ParentId": null,
            "UniqueKey": "GUE",
            "DisplayLabel": "Guest"
        },
        {
            "Id": 2,
            "ParentId": 1,
            "UniqueKey": "USR",
            "DisplayLabel": "User"
        },
        {
            "Id": 3,
            "ParentId": 2,
            "UniqueKey": "ADM",
            "DisplayLabel": "Administrator"
        }
    ]
}

The code above shows kind of the default configuration. It enables the authorization using this package and ensures that users always will be automatically created whenever they "arrive" at the API as bearer tokens. For this it will use a role assignment with the key specicfied which in turn must be one of the unique keys you specified in the RequiredRoles section. The UseCaching option switches memory caching of users on using the CacheTimeoutSeconds as timeout.

The RequiredRoles section is used to define which roles should be available in the database. The ParentId allows you to create a hierarchy here. In the sample shown all admins will be members of users als which in turn are always implcitly members of the guest.

If you are using EF Core migrations using the IDesignTimeDbContextFactory pattern you need to ensure that this is passed to your context instance there as follows:

public MyContext CreateDbContext(string[] args)
{
    var options = new DbContextOptionsBuilder<MyContext>().UseSqlServer(
            "CONNECTIONSTRING",
            o =>
            {
                o.MigrationsHistoryTable("MigrationHistory", "SystemData");
                o.CommandTimeout(3600);
            })
        .Options;
    return new MyContext(
        options,
        Options.Create(
            new EntityFrameworkAuthorizationOptions
            {
                AutoCreateNewUsers = true,
                AutoCreateRoleKey = "GUE",
                RequiredRoles = new RoleModel[]
                {
                    new()
                    {
                        Id = 1,
                        UniqueKey = "GUE",
                        DisplayLabel = "Guest"
                    },
                    new()
                    {
                        Id = 2,
                        ParentId = 1,
                        UniqueKey = "USR",
                        DisplayLabel = "User"
                    },
                    new()
                    {
                        Id = 3,
                        ParentId = 2,
                        UniqueKey = "ADM",
                        DisplayLabel = "Administrator"
                    }
                },
                IsEnabled = true
            }));
}

Also make sure, that your MyContext override of OnModelCreating calls the parents method in any case:

/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    // your code here
}

Afterwards you need to create a new migration if you are using code first:

dotnet ef migrations add "AuthorizationStructure"
dotnet ef database update

Whereever you are performing application startup is the place where you now generate 3 implementations:

public class DefaultAuthorizationMiddlewareResultHandler : BaseSimpleAuthorizationMiddlewareResultHandler<MyContext>
{
    #region constructors and destructors

    /// <inheritdoc />
    public DefaultAuthorizationMiddlewareResultHandler(
        AuthOptions authorizationOptions,
        IAuthorizedUserRepository<long, User, Role, UserRole, UserModel> repository) : base(
        authorizationOptions,
        repository)
    {
    }

    #endregion
}

public class DefaultAuthorizedUserRepository : BaseSimpleAuthorizedUserRepository<MyContext>
{
    #region constructors and destructors

    /// <inheritdoc />
    public DefaultAuthorizedUserRepository(
        IServiceProvider serviceProvider,
        MyContext dbContext,
        IMapper mapper) : base(serviceProvider, dbContext, mapper)
    {
    }

    #endregion
}

public class DefaultRolesAuthorizationHandler : BaseSimpleAuthorizationHandler<MyContext>
{
    #region constructors and destructors

    /// <inheritdoc />
    public DefaultRolesAuthorizationHandler(
        IAuthorizedUserRepository<long, User, Role, UserRole, UserModel> repository) : base(repository)
    {
    }

    #endregion
}

Now you can call the DI configuration using these types:

builder.Services
    .ConfigureAuthorizationDefaults<DefaultAuthorizationMiddlewareResultHandler, DefaultRolesAuthorizationHandler,
        DefaultAuthorizedUserRepository, MyContext>();

To test this you could add a simple controller like

[ApiController]
[Authorize]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class UserController : ControllerBase
{

    public UserController(ISimpleAuthorizedUserRepository repository)
    {
        Repository = repository;
    }

    [HttpGet("Me")]
    [Authorize(Roles = "GUE")]
    public async ValueTask<ActionResult<UserModel?>> GetAsync()
    {
        var userData = await Repository.GetOrCreateUserAsync();
        return Ok(userData);
    }

    [Injected]
    private ISimpleAuthorizedUserRepository Repository { get; } = default!;
}

If you call this method in an authorized session it should automatically detect the context user and create a new entry in the default role in your database.

About DEVDEER

DEVDEER is a company from Magdeburg, Germany which is specialized in consulting, project management and development. We are very focussed on Microsoft Azure as a platform to run our products and solutions in the cloud. So all of our backend components usually runs in this environment and is developed using .NET. In the frontend area we are using react and a huge bunch of packages to build our UIs.

You can find us online:

Website

GitHub GitHub Org's stars

YouTube YouTube Channel Subscribers YouTube Channel Views

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.  net9.0 is compatible.  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. 
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
13.0.0 29 10/18/2025
12.0.8 148 10/15/2025
12.0.7 98 10/11/2025
12.0.6 94 10/11/2025
12.0.5 98 10/11/2025
12.0.4 155 10/7/2025
12.0.3 155 10/7/2025
12.0.2 163 10/6/2025
12.0.1 158 10/4/2025
12.0.0 103 9/27/2025
11.0.5 243 8/28/2025
11.0.4 187 8/13/2025
11.0.3 128 7/29/2025
11.0.2 128 7/29/2025
11.0.1 179 7/17/2025
11.0.0 173 7/16/2025
10.0.5 337 6/11/2025
10.0.4 144 5/30/2025
10.0.3 236 5/16/2025
10.0.2 237 5/16/2025
10.0.1 250 5/16/2025
10.0.0 194 4/11/2025
9.0.2 178 4/11/2025
9.0.1 194 3/18/2025
9.0.0 162 2/14/2025
8.1.1 180 1/6/2025
8.1.0 188 1/4/2025
8.0.1 190 12/13/2024
8.0.0 170 12/11/2024
7.7.2 441 10/27/2024
7.7.1 170 10/12/2024
7.7.0 170 10/3/2024
7.6.0 225 8/23/2024
7.5.0 261 8/2/2024
7.4.5 179 6/27/2024
7.4.4 285 6/12/2024
7.4.3 179 6/12/2024
7.4.2 166 6/12/2024
7.4.1 189 6/1/2024
7.4.0 168 6/1/2024
7.3.2 474 5/29/2024
7.3.1 194 5/29/2024
7.3.0 179 5/29/2024
7.2.2 203 5/28/2024
7.2.1 195 5/27/2024
7.2.0 179 5/27/2024
7.1.0 182 5/27/2024
7.0.2 204 5/27/2024
7.0.1 196 5/25/2024
7.0.0 179 5/24/2024
6.0.0 240 5/14/2024
5.1.0 284 5/4/2024
5.0.11 172 4/19/2024
5.0.9 350 3/11/2024
5.0.8 268 3/9/2024
5.0.7 323 2/18/2024
5.0.6 457 1/18/2024
5.0.5 609 12/25/2023
5.0.4 477 12/25/2023
5.0.3 430 12/25/2023
5.0.2 478 12/11/2023
5.0.1 448 12/10/2023
5.0.0 488 12/10/2023
4.4.8 541 10/29/2023
4.4.7 482 10/29/2023
4.4.6 521 10/29/2023
4.4.5 589 10/15/2023
4.4.4 525 10/15/2023
4.4.3 553 10/15/2023
4.4.2 520 10/13/2023
4.4.0 532 10/7/2023
4.3.1 528 9/27/2023
4.3.0 574 9/16/2023
4.2.0 617 9/14/2023
4.1.0 580 9/14/2023
4.0.8 621 9/8/2023
4.0.7 567 9/7/2023
4.0.5 598 9/6/2023
4.0.3 627 9/6/2023
4.0.2 595 9/6/2023
4.0.1 590 9/6/2023
4.0.0 602 9/6/2023
3.2.1 660 8/15/2023
3.2.0 608 8/15/2023
3.1.1 678 5/29/2023
3.1.0 694 5/29/2023
3.0.3 687 5/28/2023
3.0.2 678 5/28/2023
3.0.1 732 5/24/2023
3.0.0 683 5/24/2023
2.0.1 671 5/23/2023
2.0.0 704 5/23/2023
1.0.3 727 5/21/2023
1.0.2 683 5/21/2023
1.0.0 715 5/21/2023
0.5.4-beta 665 4/12/2023
0.5.3-beta 688 4/12/2023
0.5.2-beta 692 3/11/2023
0.5.1-beta 697 1/13/2023
0.5.0-beta 695 12/29/2022
0.4.2-beta 671 12/26/2022
0.4.1-beta 701 12/6/2022
0.4.0-beta 692 11/10/2022
0.3.2-beta 743 9/30/2022
0.3.1-beta 691 9/13/2022
0.3.0-beta 715 9/13/2022
0.2.0-beta 766 9/13/2022
0.1.4-beta 679 9/7/2022
0.1.3-beta 724 9/7/2022
0.1.2-beta 701 9/7/2022
0.1.1-beta 722 9/7/2022
0.1.0-beta 700 9/6/2022
0.0.8-beta 716 9/6/2022
0.0.7-beta 684 9/6/2022
0.0.6-beta 665 9/6/2022
0.0.3-beta 699 9/6/2022
0.0.2-beta 709 9/6/2022
0.0.1-beta 714 9/5/2022

- BREAKING: Configuration key now is 'DatabaseAuthorization',
- Package updates.