AppRoles 1.0.1

dotnet add package AppRoles --version 1.0.1
                    
NuGet\Install-Package AppRoles -Version 1.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="AppRoles" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AppRoles" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="AppRoles" />
                    
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 AppRoles --version 1.0.1
                    
#r "nuget: AppRoles, 1.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 AppRoles@1.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=AppRoles&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=AppRoles&version=1.0.1
                    
Install as a Cake Tool

AppRoles

AppRoles ermöglicht das zentrale Definieren von Benutzerrollen in der appsettings.json und erstellt für jeden authentifizierten Benutzer automatisch eine Identity mit allen konfigurierten Rollen. Die Authentifizierung läuft über Windows Auth, wodurch sich die Lösung optimal für interne Unternehmensanwendungen eignet.

Übersicht

Mit AppRoles können Rollen flexibel und zentral in der Konfigurationsdatei (appsettings.json) gepflegt werden. Jeder Benutzer, der sich per Windows Auth anmeldet, erhält automatisch alle in der Konfiguration hinterlegten Rollen. Das erleichtert die Verwaltung und das Rollout von Berechtigungen.

Features

  • Zentrale Rollenverwaltung per appsettings.json
  • Automatische Zuordnung aller Rollen bei Anmeldung
  • Identity-Objekt enthält alle konfigurierten Rollen
  • Windows Authentifizierung (Integrated Windows Authentication)
  • Einfache Integration in .NET 8-Anwendungen
  • Bereitstellung als NuGet-Paket

Technologie & Architektur

  • .NET 8
  • appsettings.json als Konfigurationsquelle für Rollen
  • Windows Authentication (IIS, Kestrel, Self-Hosting)
  • Rollen werden bei jedem Login ausgelesen und der User-Identity zugeordnet

Projektstruktur

.
├─ src/                 # Quellcode
├─ appsettings.json     # Rollen-Konfiguration
├─ Program.cs           # Einstiegspunkt
├─ README.md
└─ ...

Voraussetzungen

  • .NET 8 SDK
  • Windows-Umgebung (für Windows Auth)
  • IIS oder Kestrel

Schnellstart

  1. NuGet-Paket installieren
dotnet add package AppRoles
  1. Rollen in der appsettings.json konfigurieren:

    {
      "Roles": {
        "Admin": ["Alice", "Bob"],
        "Editor": ["Charlie", "Dana"],
        "Viewer": ["Eve"]
      }
    }
    

    Domain-Umgebungen:

     {
       "Roles": {
         "Admin": ["DOMAIN\\Alice", "DOMAIN\\Bob"],
         "Editor": ["DOMAIN\\Charlie", "DOMAIN\\Dana"],
         "Viewer": ["DOMAIN\\Eve"]
       }
     }
    
  2. Anwendung konfigurieren:

    builder.Services.AddAppRoles(options => {
     options.IdentityName = "AppRoles-Identity"; //Identity-Name für die neue Identity
     options.ConfigurationSection = builder.Configuration.GetSection("Roles"); //Konfigurations-Sektion, aus der ausgelesen werden soll
    });
    
    ...
    
    app.UseAppRoles();
    
  3. Anwendung starten:

    dotnet restore
    dotnet build
    dotnet run
    
  4. Anmeldung erfolgt automatisch via Windows Auth. Jeder angemeldete User erhält alle in Roles definierten Rollen (basierend auf seinem Namen).

Beispiele

Beispiel 1: ASP.NET Core MVC (.NET 8)

1. NuGet-Paket installieren

dotnet add package AppRoles

2. Rollen in appsettings.json definieren

{
  "Roles": {
    "Admin": ["Alice", "Bob"],
    "Editor": ["Charlie", "Dana"],
    "Viewer": ["Eve"]
  }
}

3. In Program.cs integrieren

using AppRoles.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAppRoles(options => {
    options.IdentityName = "AppRoles-Identity"; //Identity-Name für die neue Identity
    options.ConfigurationSection = builder.Configuration.GetSection("Roles"); //Konfigurations-Sektion, aus der ausgelesen werden soll
});

builder.Services.AddControllersWithViews();

var app = builder.Build();

if (!app.Environment.IsDevelopment()) {
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAppRoles(); //<-- Muss nach app.UseRouting() und vor app.MapControllers() aufgerufen werden

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Beispiel 2: Minimal API (.NET 8)

1. NuGet-Paket installieren

dotnet add package AppRoles

2. Rollen in appsettings.json definieren
(siehe oben)

3. In Program.cs integrieren

using AppRoles.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAppRoles(options => {
    options.IdentityName = "AppRoles-Identity"; //Identity-Name für die neue Identity
    options.ConfigurationSection = builder.Configuration.GetSection("Roles"); //Konfigurations-Sektion, aus der ausgelesen werden soll
});

// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
    app.MapOpenApi();
}

app.UseAppRoles(); //<-- Diese Zeile ist neu
app.UseHttpsRedirection();

Hinweise zur Nutzung

  • Die Rollen werden bei jedem Login aus der Konfiguration ausgelesen und dem angemeldeten Benutzer als Claims hinzugefügt.
  • Windows Auth wird unterstützt, aber das Paket kann auch mit anderen Authentifizierungsmethoden kombiniert werden.
  • Die Claims können im Code bequem aus ClaimsPrincipal ausgelesen werden.

Lizenz

MIT – siehe LICENSE.

Kontakt


Hinweis:
Die Lösung ist für Windows Auth konzipiert.

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 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. 
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.1 215 8/17/2025
1.0.0 247 8/17/2025 1.0.0 is deprecated because it is no longer maintained.