Luval.AuthMate.Sqlite
1.1.14
dotnet add package Luval.AuthMate.Sqlite --version 1.1.14
NuGet\Install-Package Luval.AuthMate.Sqlite -Version 1.1.14
<PackageReference Include="Luval.AuthMate.Sqlite" Version="1.1.14" />
paket add Luval.AuthMate.Sqlite --version 1.1.14
#r "nuget: Luval.AuthMate.Sqlite, 1.1.14"
// Install Luval.AuthMate.Sqlite as a Cake Addin #addin nuget:?package=Luval.AuthMate.Sqlite&version=1.1.14 // Install Luval.AuthMate.Sqlite as a Cake Tool #tool nuget:?package=Luval.AuthMate.Sqlite&version=1.1.14
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.
- Create the WebApplication Builder:
var builder = WebApplication.CreateBuilder(args);
- 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();
- 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();
}
);
- 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",
});
- Build the Application:
var app = builder.Build();
- 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();
- 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
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 | Versions 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. |
-
net9.0
- Luval.AuthMate (>= 1.1.14)
- Microsoft.EntityFrameworkCore.Sqlite.Core (>= 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Luval.AuthMate.Sqlite:
Package | Downloads |
---|---|
Luval.WorkMate
WorkMate is an intelligent assistant designed to help you stay organized, proactive, and efficient in managing your personal and professional tasks |
GitHub repositories
This package is not used by any popular GitHub repositories.