devdeer.Libraries.Repository.EntityFrameworkCore.Authorized 13.0.1

dotnet add package devdeer.Libraries.Repository.EntityFrameworkCore.Authorized --version 13.0.1
                    
NuGet\Install-Package devdeer.Libraries.Repository.EntityFrameworkCore.Authorized -Version 13.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="devdeer.Libraries.Repository.EntityFrameworkCore.Authorized" Version="13.0.1" />
                    
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.1" />
                    
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.1
                    
#r "nuget: devdeer.Libraries.Repository.EntityFrameworkCore.Authorized, 13.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.
#:package devdeer.Libraries.Repository.EntityFrameworkCore.Authorized@13.0.1
                    
#: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.1
                    
Install as a Cake Addin
#tool nuget:?package=devdeer.Libraries.Repository.EntityFrameworkCore.Authorized&version=13.0.1
                    
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.1 155 10/21/2025
13.0.0 105 10/18/2025
12.0.8 162 10/15/2025
12.0.7 105 10/11/2025
12.0.6 102 10/11/2025
12.0.5 107 10/11/2025
12.0.4 174 10/7/2025
12.0.3 174 10/7/2025
12.0.2 170 10/6/2025
12.0.1 165 10/4/2025
12.0.0 108 9/27/2025
11.0.5 250 8/28/2025
11.0.4 192 8/13/2025
11.0.3 131 7/29/2025
11.0.2 131 7/29/2025
11.0.1 182 7/17/2025
11.0.0 178 7/16/2025
10.0.5 342 6/11/2025
10.0.4 147 5/30/2025
10.0.3 240 5/16/2025
10.0.2 242 5/16/2025
10.0.1 255 5/16/2025
10.0.0 198 4/11/2025
9.0.2 182 4/11/2025
9.0.1 198 3/18/2025
9.0.0 167 2/14/2025
8.1.1 184 1/6/2025
8.1.0 191 1/4/2025
8.0.1 193 12/13/2024
8.0.0 175 12/11/2024
7.7.2 444 10/27/2024
7.7.1 173 10/12/2024
7.7.0 176 10/3/2024
7.6.0 228 8/23/2024
7.5.0 264 8/2/2024
7.4.5 184 6/27/2024
7.4.4 289 6/12/2024
7.4.3 182 6/12/2024
7.4.2 171 6/12/2024
7.4.1 194 6/1/2024
7.4.0 172 6/1/2024
7.3.2 477 5/29/2024
7.3.1 197 5/29/2024
7.3.0 182 5/29/2024
7.2.2 206 5/28/2024
7.2.1 200 5/27/2024
7.2.0 183 5/27/2024
7.1.0 185 5/27/2024
7.0.2 208 5/27/2024
7.0.1 200 5/25/2024
7.0.0 182 5/24/2024
6.0.0 244 5/14/2024
5.1.0 288 5/4/2024
5.0.11 176 4/19/2024
5.0.9 353 3/11/2024
5.0.8 271 3/9/2024
5.0.7 327 2/18/2024
5.0.6 461 1/18/2024
5.0.5 614 12/25/2023
5.0.4 481 12/25/2023
5.0.3 434 12/25/2023
5.0.2 482 12/11/2023
5.0.1 451 12/10/2023
5.0.0 491 12/10/2023
4.4.8 545 10/29/2023
4.4.7 484 10/29/2023
4.4.6 523 10/29/2023
4.4.5 592 10/15/2023
4.4.4 527 10/15/2023
4.4.3 554 10/15/2023
4.4.2 522 10/13/2023
4.4.0 535 10/7/2023
4.3.1 529 9/27/2023
4.3.0 576 9/16/2023
4.2.0 618 9/14/2023
4.1.0 581 9/14/2023
4.0.8 622 9/8/2023
4.0.7 569 9/7/2023
4.0.5 599 9/6/2023
4.0.3 630 9/6/2023
4.0.2 597 9/6/2023
4.0.1 591 9/6/2023
4.0.0 603 9/6/2023
3.2.1 661 8/15/2023
3.2.0 610 8/15/2023
3.1.1 682 5/29/2023
3.1.0 697 5/29/2023
3.0.3 690 5/28/2023
3.0.2 681 5/28/2023
3.0.1 736 5/24/2023
3.0.0 686 5/24/2023
2.0.1 674 5/23/2023
2.0.0 707 5/23/2023
1.0.3 730 5/21/2023
1.0.2 686 5/21/2023
1.0.0 719 5/21/2023
0.5.4-beta 670 4/12/2023
0.5.3-beta 691 4/12/2023
0.5.2-beta 695 3/11/2023
0.5.1-beta 700 1/13/2023
0.5.0-beta 699 12/29/2022
0.4.2-beta 673 12/26/2022
0.4.1-beta 705 12/6/2022
0.4.0-beta 695 11/10/2022
0.3.2-beta 747 9/30/2022
0.3.1-beta 694 9/13/2022
0.3.0-beta 718 9/13/2022
0.2.0-beta 773 9/13/2022
0.1.4-beta 685 9/7/2022
0.1.3-beta 730 9/7/2022
0.1.2-beta 705 9/7/2022
0.1.1-beta 726 9/7/2022
0.1.0-beta 706 9/6/2022
0.0.8-beta 719 9/6/2022
0.0.7-beta 687 9/6/2022
0.0.6-beta 671 9/6/2022
0.0.3-beta 703 9/6/2022
0.0.2-beta 713 9/6/2022
0.0.1-beta 718 9/5/2022

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