Luval.AuthMate 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Luval.AuthMate --version 1.0.5                
NuGet\Install-Package Luval.AuthMate -Version 1.0.5                
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.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Luval.AuthMate --version 1.0.5                
#r "nuget: Luval.AuthMate, 1.0.5"                
#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.0.5

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

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.
  • 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.

Configuring the Sql Database

The project uses EntityFramework, there is a SQL script for a Postgresql database here if you need to implement the database storage for any database engine here is an example, just extend the AuthMateContext class here is an example

public class PostgresAuthMateContext : AuthMateContext, IAuthMateContext
{
    private string _connString;

    /// <summary>
    /// Creates a new instance of the context
    /// </summary>
    /// <param name="connectionString">A valid postgres connection string</param>
    public PostgresAuthMateContext(string connectionString)
    {
            _connString = connectionString;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(_connString);
        if(Debugger.IsAttached)
            optionsBuilder.LogTo(Console.WriteLine);
    }
}

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

Configuring your application

To configure the Program.cs file for the AuthMate project, follow these steps:

  1. Add Required Services: Register the necessary services for the DbContext and authentication.
  2. Configure the DbContext: Set up the DbContext with the appropriate connection string.
  3. Configure Authentication: Set up authentication schemes and options. Here is an example configuration for Program.cs:
using Luval.AuthMate.Core.Interfaces;
using Luval.AuthMate.Infrastructure;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();

// Configure DbContext
builder.Services.AddDbContext<IAuthMateContext, AuthMateContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("AuthMateConnection")));

// Configure authentication
builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = "Cookies";
    options.DefaultChallengeScheme = "Cookies";
})
.AddCookie("Cookies", options =>
{
    options.LoginPath = "/Account/Login";
    options.LogoutPath = "/Account/Logout";
});

// Add other necessary services
builder.Services.AddScoped<IAuthService, AuthService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

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

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

Create a Controller in your Web Application

In order to handle the authentication request you will need to create a controller. here is a simple working code

[Route("/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
    [AllowAnonymous]
    [HttpGet("google-login")] //This mathches the configuration of the Google Auth
    public IActionResult GoogleLogin()
    {

        // Adds the properties ad redirect information
        // this could be change to include a redirect as part
        // of a query string if required
        var prop = new AuthenticationProperties()
        {
            RedirectUri = "/"
        };

        // Creates tthe challange
        var challange = Challenge(prop, GoogleDefaults.AuthenticationScheme);

        return challange;
    }

    [AllowAnonymous]
    [HttpGet("logout")]
    public async Task<IActionResult> Logout()
    {
        // Signout the user from the OAuth flow
        await HttpContext.SignOutAsync();

        // Redirect to root so that when logging back in, it takes to home page
        return Redirect("/");
    }

}

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 80 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 90 1/5/2025
1.1.8 143 1/5/2025
1.1.7 99 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