MiniAuth - 轻量级.Net开源身份认证中间件

文章目录

.NetCore身份认证组件MiniAuth

安装MiniAuth组件

install-package MiniAuth

在Startup/Program配置和启用MiniAuth

csharp 复制代码
            var builder = WebApplication.CreateBuilder(args);

            //注入MiniAuth(越早注入越好)
            builder.Services.AddMiniAuth(miniAuthOption =>
            {
                //修改认证类型
                miniAuthOption.AuthenticationType = AuthType.BearerJwt;
                //修改秘钥
                miniAuthOption.JWTKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("cvzRqFw9m7HPNePy"));
                //修改MiniAuth数据库链接
                miniAuthOption.SqliteConnectionString = "Source=lingbug_miniauth_identity.db";
            });

            builder.Services.AddControllers();
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var app = builder.Build();

            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();
            app.UseAuthorization();
            app.MapControllers();

            app.Run();

MiniAuth默认配置

csharp 复制代码
    internal class MiniAuthOption
    {
        public static string RoutePrefix = "MiniAuth";
        public static string LoginPath = $"/{RoutePrefix}/login.html";
        public static bool DisableMiniAuthLogin = false;

        public static AuthType AuthenticationType = AuthType.Cookie;
        public static SecurityKey JWTKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this is miniauth key for demo"));
        /// <summary>
        /// Token expires in seconds, default is 1 hour
        /// </summary>
        public static int TokenExpiresIn = 60 * 60;
        public static string Issuer = $"{RoutePrefix}";
        public static string SqliteConnectionString = "Data Source=miniauth_identity.db";
    }

MiniAuth认证类型枚举

csharp 复制代码
    public enum AuthType
    {
        Cookie,
        BearerJwt
    }

配置完成,启动项目,访问管理界面

  • 地址:http://localhost:5025/MiniAuth/index.html
  • 注意:首次访问会提示未授权,登录后即可正常访问
  • 登录管理界面账号密码:
  • 账号:admin@mini-software.github.io
  • 密码:E7c4f679-f379-42bf-b547-684d456bc37f(请及时修改密码)

管理界面的端口(Endpoints 管理)

名称 路由
HTTP: POST /MiniAuth/refreshToken /MiniAuth/refreshToken
HTTP: POST /MiniAuth/login /MiniAuth/login
HTTP: POST /MiniAuth/api/saveUser /MiniAuth/api/saveUser
HTTP: POST /MiniAuth/api/saveRole /MiniAuth/api/saveRole
HTTP: POST /MiniAuth/api/resetPassword /MiniAuth/api/resetPassword
HTTP: POST /MiniAuth/api/getUsers /MiniAuth/api/getUsers
HTTP: POST /MiniAuth/api/getConfig /MiniAuth/api/getConfig
HTTP: POST /MiniAuth/api/deleteUser /MiniAuth/api/deleteUser
HTTP: POST /MiniAuth/api/deleteRole /MiniAuth/api/deleteRole
HTTP: GET /MiniAuth/logout /MiniAuth/logout
HTTP: GET /MiniAuth/api/getUserInfo /MiniAuth/api/getUserInfo
HTTP: GET /MiniAuth/api/getRoles /MiniAuth/api/getRoles
HTTP: GET /MiniAuth/api/getAllEndpoints /MiniAuth/api/getAllEndpoints
HTTP: GET /MiniAuth/AccessDenied /MiniAuth/AccessDenied
相关推荐
liyongqiangcc5 小时前
了解 ASP.NET Core 中的中间件
.net
Kiros_Jiang8 小时前
开源低代码平台-Microi吾码 打印引擎使用
javascript·开源·json·.net·pip
web1478621072311 小时前
C# .Net Web 路由相关配置
前端·c#·.net
数据的世界011 天前
.NET开发人员学习书籍推荐
学习·.net
paixiaoxin1 天前
CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究
人工智能·深度学习·机器学习·生成对抗网络·计算机视觉·ocr·.net
1900431 天前
.NET重点
.net
m0_663234011 天前
在 .NET 5.0 运行 .NET 8.0 教程:使用 ASP.NET Core 创建 Web API
前端·asp.net·.net
小码编匠2 天前
.NET 下 RabbitMQ 队列、死信队列、延时队列及小应用
后端·c#·.net
真想骂*2 天前
vmime.net_4.dll详解:它是什么,有何用途?
.net