Luval.AuthMate 1.1.14

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

// Install Luval.AuthMate as a Cake Tool
#tool nuget:?package=Luval.AuthMate&version=1.1.14                

Actions Status Badge

AuthMate: Simplify Authentication and Authorization for Your Apps

AuthMate is a comprehensive authentication and authorization system designed to manage user accounts, roles, and permissions within a Blazor application. It provides a robust and flexible framework for handling user authentication, including support for OAuth providers, user roles, and login history tracking.

Key Features

  • Social Login Integration: Quickly enable login via Google, Microsoft, and Facebook with minimal setup.
  • SQL Backend Support: Manage users, roles, and permissions seamlessly using your existing SQL database
    • Support for Postgres and Sqlite, easily extensible for other providers with minimal code
  • Provides capabilities for multi tenency, if desired using the concept of Accounts, multiple users can be assigned to an Account
  • Account Management:
    • Assign users to one or multiple accounts.
    • Support for account types to accommodate different use cases.
    • Role-Based Access Control (RBAC): Define roles and permissions for users at the account level.
    • Developer-Friendly: Designed for quick implementation and scalability, saving you hours of development time.

Installation

To install the AuthMate library, add the following NuGet package to your project:

dotnet add package Luval.AuthMate

Getting Google Client Id and Client Secret

To configure the OAuth you need to get the information from google follow the steps in this article https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-8.0

Getting started with the configuration of the AuthMate Library

This guide provides the initial configuration steps for setting up the AuthMate library in your .NET application. AuthMate is designed to manage authentication and authorization flows, including OAuth 2.0 integration.

Configuration Settings

First, add the necessary configuration settings to your appsettings.json file. These settings include OAuth provider details and AuthMate-specific keys.

appsettings.json
{
  "OAuthProviders": {
    "Google": {
      "ClientID": "your-client-id",
      "OwnerEmail": "your-email",
      "AuthorizationEndpoint": "https://accounts.google.com/o/oauth2/v2/auth",
      "TokenEndpoint": "https://oauth2.googleapis.com/token",
      "UserInfoEndpoint": "https://www.googleapis.com/oauth2/v3/userinfo",
      "CodeFlowRedirectUri": "your-app.com/callback",
      "Scopes": "https://www.googleapis.com/auth/gmail.readonly"
      "OwnerEmail": "mainuser@gmail.com" // This is the email of the owner of the project
    }
  }
}
secrets.json

Update the secrets for the OAuth flows, here is an article on how to handle secrets Safe storage of app secrets

{
    "OAuthProviders:Google:ClientSecret": "your-secret",
    "AuthMate:BearingTokenKey": "your-key"
}

Program.cs Configuration

In your Program.cs file, configure the services and middleware required for AuthMate.

  1. Create the WebApplication Builder:
    var builder = WebApplication.CreateBuilder(args);
  1. Add Services to the Container:
    // Add Razor components and Fluent UI components
    builder.Services.AddRazorComponents()
        .AddInteractiveServerComponents();
    builder.Services.AddFluentUIComponents();

    // Add logging services (required dependency for AuthMate)
    builder.Services.AddLogging();

    // Add controllers, HTTP client, and context accessor
    builder.Services.AddControllers();
    builder.Services.AddHttpClient();
    builder.Services.AddHttpContextAccessor();
    
  1. Add AuthMate Services:
    var config = builder.Configuration;

    builder.Services.AddAuthMateServices(
        // The key to use for the bearing token implementation
        config["AuthMate:BearingTokenKey"],
        (s) => {
            // Returns a local instance of Sqlite
            // Replace this with your own implementation of Postgres, MySql, SqlServer, etc.
            return new SqliteAuthMateContext();
        }
    );
    
  1. Add AuthMate Google OAuth Provider:
    builder.Services.AddAuthMateGoogleAuth(new GoogleOAuthConfiguration()
    {
        // Client ID from your config file
        ClientId = config["OAuthProviders:Google:ClientId"] ?? throw new ArgumentNullException("The Google client id is required"),
        // Client secret from your config file
        ClientSecret = config["OAuthProviders:Google:ClientSecret"] ?? throw new ArgumentNullException("The Google client secret is required"),
        // Set the login path in the controller and pass the provider name
        LoginPath = "/api/auth",
    });
    
  1. Build the Application:
    var app = builder.Build();
  1. Configure the HTTP Request Pipeline
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseAntiforgery();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();

    app.MapControllers();
    app.MapRazorComponents<App>()
        .AddInteractiveServerRenderMode();
    
  1. Initialize the Database
    var contextHelper = new AuthMateContextHelper(
        new SqliteAuthMateContext(),
        new ColorConsoleLogger<AuthMateContextHelper>()
    );

    // Ensure the database is created and initialize it with the owner email and required initial records
    contextHelper.InitializeDbAsync(config["OAuthProviders:Google:OwnerEmail"] ?? "")
        .GetAwaiter()
        .GetResult();
    

Summary

By following these steps, you will have configured the AuthMate library in your .NET application, enabling OAuth 2.0 authentication and authorization flows. Make sure to replace placeholder values in the configuration with your actual credentials and settings. Here is a complete example of a complete implementation Program.cs

Database Information

The database has been implemented in postgres but can be very easily implemented in any other database engine that supports Entity framework, all you need to do is to extend this class AuthMateContext.cs

ERD Model

ERD Model


Sample Data

  • Default account type: Free
  • Predefined roles: Administrator, Owner, Member, Visitor.
  • Pre-authorized user: oscar.marin.saenz@gmail.com (associated with the "Free" account type).
Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Luval.AuthMate:

Package Downloads
Luval.GenAIBotMate

GenAIBotMate is a C# library that enables developers to interface with Large Language Models (LLMs), persist chat conversations, and provide an API to retrieve past interactions. The library also supports persisting uploaded files, including images, to cloud storage providers like Azure Blob Storage

Luval.AuthMate.Postgres

Package Description

Luval.AuthMate.Blazor

AuthMate is a powerful yet easy-to-use NuGet package designed to streamline user authentication and authorization for web and mobile applications

Luval.AuthMate.Sqlite

Package Description

Luval.WorkMate.Components

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.14 78 1/28/2025
1.1.13 123 1/13/2025
1.1.12 90 1/12/2025
1.1.11 123 1/7/2025
1.1.10 92 1/6/2025
1.1.9 88 1/5/2025
1.1.8 143 1/5/2025
1.1.7 97 1/5/2025
1.1.6 111 1/5/2025
1.1.5 123 1/4/2025
1.1.4 122 1/4/2025
1.1.3 105 1/3/2025
1.1.1 194 12/24/2024
1.0.11 89 12/24/2024
1.0.10 84 12/23/2024
1.0.9 87 12/23/2024
1.0.8 88 12/23/2024
1.0.7 88 12/23/2024
1.0.6 90 12/23/2024
1.0.5 83 12/22/2024
1.0.4 78 12/22/2024
1.0.3 77 12/21/2024
1.0.0 89 12/16/2024

- Upgraded to Framework 9.0
- Added support for refresh tokens