Code Monkey home page Code Monkey logo

miniauth's Introduction

NuGet star GitHub stars



Your Star, Donate, Recomm. can make MiniAuth better

Introduction

"One-line code" adds identity management web for your new/old projects

Image 1 Image 2
Image 3 Image 4

Features

  • Compatibility: Based JWT, Cookie, and Session that follow the .NET Identity standard.
  • Out of the box: Easy integration, MiniAuth works with APIs, MVC, Razor Pages.
  • Multi-platform: Supports Linux, macOS environments.
  • Multiple Database Support: Compatible with any database that follow the Identity EF Core standard.

Installation

Install the package from NuGet:

dotnet add package MiniAuth
// or
NuGet\Install-Package MiniAuth

Quick Start

Add a single line of code services.AddMiniAuth() in Startup, then run your project. Example:

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddMiniAuth();

        var app = builder.Build();
        app.Run();
    }
}

The default admin account is [email protected] with the password E7c4f679-f379-42bf-b547-684d456bc37f (remember to change the password). The admin page can be accessed at http(s)://yourhost/miniauth/index.html.

Note: If you already have your own identity auth, please follow the instructions below.

Existing Identity Setup

Turn off autoUse for AddMiniAuth, and replace it with your own IdentityDBContext, user, and permission authentication in UseMiniAuth, placing it after your own Auth. Example:

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
    builder.Services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(connectionString));
    builder.Services.AddDatabaseDeveloperPageExceptionFilter();

    builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
        .AddRoles<IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

    builder.Services.AddControllersWithViews();

    builder.Services.AddMiniAuth(autoUse: false); // <= ❗❗❗

    var app = builder.Build();

    app.UseMiniAuth<ApplicationDbContext, IdentityUser, IdentityRole>();  // <= ❗❗❗ 
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.MapRazorPages();

    app.Run();
}

Order Matters

Please place UseMiniAuth after routing generation; otherwise, the system won't be able to obtain routing data for permission checks, like:

app.UseRouting();
app.UseMiniAuth();

Note: Adding Role Rules

Please add AddRoles<IdentityRole>(); otherwise [Authorize(Roles = "YourRole")] won't work.

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddRoles<IdentityRole>() // ❗❗❗ 
    .AddEntityFrameworkStores<ApplicationDbContext>();

Changing Databases

MiniAuth system defaults to using SQLite without any code settings required. If you need to switch, specify a different database type in app.UseMiniAuth.

Configuration and Options

Default Mode

  • MiniAuth operates in a default mode where IT Admin centrally manages user operations like registration and password reset, requiring an Admin privilege account to perform these actions. Default Role = miniauth-admin.

Login and User Validation

For non-ApiController, login redirects to the login.html page (determined by Headers["X-Requested-With"] == "XMLHttpRequest" or ApiControllerAttribute). ApiController Controllers do not redirect to the login page by default but return a 401 status code.

Distributed Systems

  • Please switch the database source to SQL Server, MySQL, PostgreSQL, etc.

Release Notes

Refer to the Release Notes for updates.

TODO

Link: MiniAuth.Identify project

miniauth's People

Contributors

shps951023 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.