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
相关推荐
WineMonk16 小时前
.NET WPF CommunityToolkit.Mvvm框架
.net·wpf·mvvm
界面开发小八哥16 小时前
界面控件DevExpress WPF中文教程:Data Grid——卡片视图设置
.net·wpf·界面控件·devexpress·ui开发
九鼎科技-Leo1 天前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo1 天前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发1 天前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
九鼎科技-Leo1 天前
在 C# 中,ICollection 和 IList 接口有什么区别?
windows·c#·.net
时光追逐者1 天前
C#/.NET/.NET Core学习路线集合,学习不迷路!
开发语言·学习·c#·asp.net·.net·.netcore·微软技术
Crazy Struggle2 天前
.NET 全功能流媒体管理控制接口平台
.net·开源项目·流媒体