diff --git a/README.zh-CN.md b/README.zh-CN.md index f92bb49..6b67400 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -35,7 +35,7 @@ MiniAuth 一个轻量 ASP.NET Core Identity Web 后台管理中间插件 -「一行代码」为「新、旧项目」 添加 Identity 系统跟用户、权限管理后台 Web UI +「一行代码」为「新、旧项目」 添加 Identity 系统跟用户、权限管理网页后台系统 开箱即用,避免打掉重写或是严重耦合情况 @@ -318,15 +318,19 @@ Response: ### 注册 -TODO +请使用 ASP.NET Core Identity 自带的注册API跟页面 ### 忘记密码 -TODO +请使用 ASP.NET Core Identity 自带的注册API跟页面 ### 获取用户信息 -TODO +请使用 ASP.NET Core Identity 自带的注册API跟页面 + + + +### 重导URI @@ -351,11 +355,25 @@ builder.Services.AddDefaultIdentity(options => options.SignIn.Requ .AddEntityFrameworkStores(); ``` +### 请自行设定好 CORS + + + + + + + #### 分布式系统 - 数据库来源请换成 SQL Server、MySQL、PostgreSQL 等数据库 - 建议更换 JWT 等 auth 方式 + + + + + + ### 更新日志 请查看 [Release Notes](releases) diff --git a/tests/MiniAuth.Identity/HomeController.cs b/tests/MiniAuth.Identity/Controllers.cs similarity index 96% rename from tests/MiniAuth.Identity/HomeController.cs rename to tests/MiniAuth.Identity/Controllers.cs index 4e7ba51..dd6a5ca 100644 --- a/tests/MiniAuth.Identity/HomeController.cs +++ b/tests/MiniAuth.Identity/Controllers.cs @@ -1,15 +1,20 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; namespace MiniAuth.Identity { + public class HomeController : Controller { + [HttpGet] [Route("/")] public ActionResult Home() => Content("This's homepage"); [HttpGet] [Route("/About")] public ActionResult About() => Content("This's About"); + [HttpGet] [Route("/UserInfo")] + [Authorize()] public ActionResult UserInfo() { var user = this.User; @@ -17,6 +22,7 @@ public ActionResult UserInfo() } } + [Authorize(Roles = "Order")] [ApiController] [Route("api/[controller]")] public class OrderController : ControllerBase @@ -86,7 +92,7 @@ public class Order } - + [Authorize(Roles = "Product")] [ApiController] [Route("api/[controller]")] public class ProductController : ControllerBase @@ -155,6 +161,7 @@ public class Product } } + [Authorize(Roles = "ProductStock")] [ApiController] [Route("api/[controller]")] public class ProductStockController : ControllerBase diff --git a/tests/MiniAuth.Identity/Program.cs b/tests/MiniAuth.Identity/Program.cs index 7f553f6..3c48147 100644 --- a/tests/MiniAuth.Identity/Program.cs +++ b/tests/MiniAuth.Identity/Program.cs @@ -1,7 +1,6 @@ -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc; -using MiniAuth.IdentityAuth.Models; +using Microsoft.IdentityModel.Tokens; using System.Diagnostics; +using System.Text; namespace MiniAuth.Identity { @@ -10,40 +9,33 @@ public class Program public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); - Debug.WriteLine("* start Services add"); - builder.Services.AddCors(options => - options.AddPolicy("AllowAll", - builder => builder - .WithOrigins( - "http://localhost:5173" - ) - .AllowAnyMethod() - .AllowAnyHeader() - .AllowCredentials() - )); + MiniAuthOptions.AuthenticationType = MiniAuthOptions.AuthType.BearerJwt; + var key = builder.Configuration["JWTKey"]; + MiniAuthOptions.JWTKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)); + builder.Services.AddCors(options => + { + options.AddPolicy("AllowAll", + builder => + { + builder + .AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader() + //.AllowCredentials() + ; + }); + }); builder.Services.AddMiniAuth(); builder.Services.AddControllers(); -#if DEBUG - //builder.Services.AddEndpointsApiExplorer(); - //builder.Services.AddSwaggerGen(); - //builder.Services.AddIdentityApiEndpoints(); -#endif + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); - Debug.WriteLine("* start builder build"); var app = builder.Build(); app.UseCors("AllowAll"); - app.MapGet("/", () => "Hello World!"); - //app.MapGroup("/api").MapIdentityApi(); app.MapControllers(); - //if (app.Environment.IsDevelopment()) - //{ - // app.UseSwagger(); - // app.UseSwaggerUI(); - //} - //app.UseMiniAuth(); + app.UseSwagger(); + app.UseSwaggerUI(); app.Run(); } } - - } diff --git a/tests/MiniAuth.Identity/appsettings.json b/tests/MiniAuth.Identity/appsettings.json index 10f68b8..1084542 100644 --- a/tests/MiniAuth.Identity/appsettings.json +++ b/tests/MiniAuth.Identity/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "JWTKey": "40b9e63e-b264-4939-9f12-60a21d96dfbf" }