Location>code7788 >text

NET open source authentication project MiniAuth goes live!

Popularity:233 ℃/2024-08-06 11:00:33

preamble

In the Web application project permissions authentication is a topic that can not be avoided , the traditional method is complex and time-consuming . MiniAuth launched specifically designed for .NET developers to design a simple , practical permissions authentication project .

MiniAuth, as a plugin for Core, allows us to quickly and easily implement user login , permission checking and other functions . It supports a variety of authentication methods , such as JWT, Cookie , and easy to integrate into existing projects .

Whether developing WebAPI or MVC applications , MiniAuth can help us quickly build a back-end management system . It is easy to use , do not change the existing database structure , and does not increase learning costs .

MiniAuth, make permissions management less cumbersome and rapid development more efficient. Try it out!

Projects

MiniAuth is a lightweight Core Identity web backend management middleware plugin.

"One line of code to add Identity system and user and permission management web backend system to new and old projects.

Out of the box, avoid knocking out rewrites or serious coupling situations.

Project Characteristics

  • compatibility : be in favor of .NET identity Based on JWT, Cookie, Session et al. (and other authors)
  • Simplicity: Plug and play design, API, MVC, Razor Page, etc. out of the box.
  • Multi-database support : Support Oracle, SQL Server, MySQL, etc. EF Core
  • Non-intrusive: does not affect existing databases, project structure
  • Multi-platform : Support Linux, macOS environment

Project use

MiniAuth is a lightweight Core Identity web backend management plugin that is relatively simple and intuitive to use.

Below is a basic usage example to help us quickly integrate MiniAuth into a Core project.

1. Install MiniAuth

First, MiniAuth needs to be installed via the NuGet package manager.

Alternatively, in Visual Studio, open the NuGet Package Manager console (or use the NuGet Package Manager UI) and execute the following command to install MiniAuth:

Install-Package MiniAuth

NET CLI can also be installed using the following command:

dotnet add package MiniAuth

2、Configure MiniAuth

After installation, MiniAuth needs to be configured in the Startup class or Program class (depending on the version of .NET Core used) of the Core project.

For .NET 6 or later, this is usually configured in the file

public class Program  
{  
    public static void Main(string[] args)  
    {  
        var builder = (args);  
  
        // Add MiniAuth service
        ();  
  
        //If a custom configuration is required, such as using JWT authentication, the
        (options =>  
        {  
             = ;  
             = new SymmetricSecurityKey(Encoding.("Your own JWT key"));  
        });  
  
        var app = ();  
  
        // Other configurations...
        ();  
    }  
}

3. Access to the management page

Once the configuration is complete, run the current project. miniAuth will automatically register the necessary routes and middleware and provide a default management interface.

You can access the management interface by visiting the following URL (please replace it according to the actual deployment)localhost:5000):

http://localhost:5000/miniauth/

On the first visit, you can use a preset administrator account

Account:admin@

Password: E7c4f679-f379-42bf-b547-684d456bc37f (Please remember to change your password)

Manage your Identity users, roles, and endpoints.

 

4、Authority management

MiniAuth provides management of users, roles and permissions. New users can be created, roles can be assigned, and permissions can be managed through the administration interface.

For APIs or pages that require permission control, access can be restricted using the [Authorize] attribute or the [Authorize(Roles = "Role Name")] attribute on the corresponding controller or method.

5. Customization and expansion

MiniAuth provides flexible extension points that can be customized according to project requirements. Customize the authentication process, user data storage, etc. by implementing or extending the interfaces and classes provided by MiniAuth.

  • MiniAuth Cookie Identity

MiniAuth is preset as a single Coookie Based identity, if you have a separate front-end and back-end project, please replace it with an Auth such as JWT.

  • MiniAuth JWT Identity

Specify the AuthenticationType as BearerJwt.

var builder = (args);
(options:(options) =>
{
     = ;
});

Please remember to customize the JWT Security Key as follows.

var builder = (args);
(options: (options) =>
{
     = new SymmetricSecurityKey(Encoding.("6ee3edbf-488e-4484-9c2c-e3ffa6dcbc09"));
});
  • MiniAuth Preset Mode

In order to centralize user management for IT Admin, user registration, password reset and other operations require Admin privilege account operation, preset Role = miniauth-admin

  • Close MiniAuth Login

If you only want to use your own login logic, pages, and APIs, you can specify the login path and turn off the switch

// Before service registration
(options: (options) =>
{
     = "/Identity/Account/Login";
     = true;
});
  • Customizing the preset SQLite Connection String
(options: (options) =>
{
     = "Data Source=miniauth_identity.db";
});
  • Customize databases, users, roles

The MiniAuth system is preconfigured to use SQLite EF Core, IdentityUser, and IdentityRole out of the box If you need to switch please do so in the The generalization specifies different databases, their own users, and role classes.

<YourDbContext, YourIdentityUser, YourIdentityRole>();

caveat

1. Make sure that the project has installed Core Identity-related packages, because MiniAuth is built on Core Identity.

2、According to the project requirements, choose the appropriate authentication method (such as JWT, Cookie, etc.).

3、Attention to the order, please put UseMiniAuth after the route generation, otherwise the system can not get the route data for permission judgment, such as.

();
();

4、Please add Role rules

Please add AddRoles<IdentityRole>(), otherwise [Authorize(Roles = "Permissions")] won't take effect

<IdentityUser>(options =>  = true)
    .AddRoles<IdentityRole>() // ❗❗❗ 
    .AddEntityFrameworkStores<ApplicationDbContext>();

Project Address

Github:/mini-software/MiniAuth

Gitee:/dotnetchina/MiniAuth

 If you found this article helpful, why not support it with a like? Your support is what keeps me motivated to continue sharing my knowledge. Feel free to leave a comment if you have any questions or need further help.

You can also join WeChat[DotNet Technician] community to share ideas and grow with other tech-loving peers!