Skip to content

Commit dc44384

Browse files
committed
Demo JWT Key config #205
1 parent 4378fbb commit dc44384

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

README.zh-CN.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
MiniAuth 一个轻量 ASP.NET Core Identity Web 后台管理中间插件
3737

38-
「一行代码」为「新、旧项目」 添加 Identity 系统跟用户、权限管理后台 Web UI
38+
「一行代码」为「新、旧项目」 添加 Identity 系统跟用户、权限管理网页后台系统
3939

4040
开箱即用,避免打掉重写或是严重耦合情况
4141

@@ -318,15 +318,19 @@ Response:
318318

319319
### 注册
320320

321-
TODO
321+
请使用 ASP.NET Core Identity 自带的注册API跟页面
322322

323323
### 忘记密码
324324

325-
TODO
325+
请使用 ASP.NET Core Identity 自带的注册API跟页面
326326

327327
### 获取用户信息
328328

329-
TODO
329+
请使用 ASP.NET Core Identity 自带的注册API跟页面
330+
331+
332+
333+
### 重导URI
330334

331335

332336

@@ -351,11 +355,25 @@ builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.Requ
351355
.AddEntityFrameworkStores<ApplicationDbContext>();
352356
```
353357

358+
### 请自行设定好 CORS
359+
360+
361+
362+
363+
364+
365+
354366
#### 分布式系统
355367

356368
- 数据库来源请换成 SQL Server、MySQL、PostgreSQL 等数据库
357369
- 建议更换 JWT 等 auth 方式
358370

371+
372+
373+
374+
375+
376+
359377
### 更新日志
360378

361379
请查看 [Release Notes](releases)

tests/MiniAuth.Identity/HomeController.cs renamed to tests/MiniAuth.Identity/Controllers.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Mvc;
23

34
namespace MiniAuth.Identity
45
{
6+
57
public class HomeController : Controller
68
{
9+
[HttpGet]
710
[Route("/")]
811
public ActionResult Home() => Content("This's homepage");
912
[HttpGet]
1013
[Route("/About")]
1114
public ActionResult About() => Content("This's About");
15+
[HttpGet]
1216
[Route("/UserInfo")]
17+
[Authorize()]
1318
public ActionResult UserInfo()
1419
{
1520
var user = this.User;
1621
return Json(user);
1722
}
1823
}
1924

25+
[Authorize(Roles = "Order")]
2026
[ApiController]
2127
[Route("api/[controller]")]
2228
public class OrderController : ControllerBase
@@ -86,7 +92,7 @@ public class Order
8692
}
8793

8894

89-
95+
[Authorize(Roles = "Product")]
9096
[ApiController]
9197
[Route("api/[controller]")]
9298
public class ProductController : ControllerBase
@@ -155,6 +161,7 @@ public class Product
155161
}
156162
}
157163

164+
[Authorize(Roles = "ProductStock")]
158165
[ApiController]
159166
[Route("api/[controller]")]
160167
public class ProductStockController : ControllerBase

tests/MiniAuth.Identity/Program.cs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using Microsoft.AspNetCore.Identity;
2-
using Microsoft.AspNetCore.Mvc;
3-
using MiniAuth.IdentityAuth.Models;
1+
using Microsoft.IdentityModel.Tokens;
42
using System.Diagnostics;
3+
using System.Text;
54

65
namespace MiniAuth.Identity
76
{
@@ -10,40 +9,33 @@ public class Program
109
public static void Main(string[] args)
1110
{
1211
var builder = WebApplication.CreateBuilder(args);
13-
Debug.WriteLine("* start Services add");
14-
builder.Services.AddCors(options =>
15-
options.AddPolicy("AllowAll",
16-
builder => builder
17-
.WithOrigins(
18-
"http://localhost:5173"
19-
)
20-
.AllowAnyMethod()
21-
.AllowAnyHeader()
22-
.AllowCredentials()
23-
));
12+
MiniAuthOptions.AuthenticationType = MiniAuthOptions.AuthType.BearerJwt;
13+
var key = builder.Configuration["JWTKey"];
14+
MiniAuthOptions.JWTKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
15+
builder.Services.AddCors(options =>
16+
{
17+
options.AddPolicy("AllowAll",
18+
builder =>
19+
{
20+
builder
21+
.AllowAnyOrigin()
22+
.AllowAnyMethod()
23+
.AllowAnyHeader()
24+
//.AllowCredentials()
25+
;
26+
});
27+
});
2428
builder.Services.AddMiniAuth();
2529
builder.Services.AddControllers();
26-
#if DEBUG
27-
//builder.Services.AddEndpointsApiExplorer();
28-
//builder.Services.AddSwaggerGen();
29-
//builder.Services.AddIdentityApiEndpoints<IdentityUser>();
30-
#endif
30+
builder.Services.AddEndpointsApiExplorer();
31+
builder.Services.AddSwaggerGen();
3132

32-
Debug.WriteLine("* start builder build");
3333
var app = builder.Build();
3434
app.UseCors("AllowAll");
35-
app.MapGet("/", () => "Hello World!");
36-
//app.MapGroup("/api").MapIdentityApi<IdentityUser>();
3735
app.MapControllers();
38-
//if (app.Environment.IsDevelopment())
39-
//{
40-
// app.UseSwagger();
41-
// app.UseSwaggerUI();
42-
//}
43-
//app.UseMiniAuth();
36+
app.UseSwagger();
37+
app.UseSwaggerUI();
4438
app.Run();
4539
}
4640
}
47-
48-
4941
}

tests/MiniAuth.Identity/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"AllowedHosts": "*"
8+
"AllowedHosts": "*",
9+
"JWTKey": "40b9e63e-b264-4939-9f12-60a21d96dfbf"
910
}

0 commit comments

Comments
 (0)