去皮卡多 发表于 2024-9-22 16:43:46

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

.NetCore身份认证组件MiniAuth

安装MiniAuth组件

   install-package MiniAuth
在Startup/Program设置和启用MiniAuth


            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默认设置


    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认证类型枚举


    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/refreshTokenHTTP: POST /MiniAuth/login/MiniAuth/loginHTTP: POST /MiniAuth/api/saveUser/MiniAuth/api/saveUserHTTP: POST /MiniAuth/api/saveRole/MiniAuth/api/saveRoleHTTP: POST /MiniAuth/api/resetPassword/MiniAuth/api/resetPasswordHTTP: POST /MiniAuth/api/getUsers/MiniAuth/api/getUsersHTTP: POST /MiniAuth/api/getConfig/MiniAuth/api/getConfigHTTP: POST /MiniAuth/api/deleteUser/MiniAuth/api/deleteUserHTTP: POST /MiniAuth/api/deleteRole/MiniAuth/api/deleteRoleHTTP: GET /MiniAuth/logout/MiniAuth/logoutHTTP: GET /MiniAuth/api/getUserInfo/MiniAuth/api/getUserInfoHTTP: GET /MiniAuth/api/getRoles/MiniAuth/api/getRolesHTTP: GET /MiniAuth/api/getAllEndpoints/MiniAuth/api/getAllEndpointsHTTP: GET /MiniAuth/AccessDenied/MiniAuth/AccessDenied
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: MiniAuth - 轻量级.Net开源身份认证中心件