Skip to content

Commit

Permalink
Demo JWT Key config #205
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Jun 17, 2024
1 parent 4378fbb commit dc44384
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
26 changes: 22 additions & 4 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

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

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

Expand Down Expand Up @@ -318,15 +318,19 @@ Response:

### 注册

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

### 忘记密码

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

### 获取用户信息

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



### 重导URI



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

### 请自行设定好 CORS







#### 分布式系统

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







### 更新日志

请查看 [Release Notes](releases)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
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;
return Json(user);
}
}

[Authorize(Roles = "Order")]
[ApiController]
[Route("api/[controller]")]
public class OrderController : ControllerBase
Expand Down Expand Up @@ -86,7 +92,7 @@ public class Order
}



[Authorize(Roles = "Product")]
[ApiController]
[Route("api/[controller]")]
public class ProductController : ControllerBase
Expand Down Expand Up @@ -155,6 +161,7 @@ public class Product
}
}

[Authorize(Roles = "ProductStock")]
[ApiController]
[Route("api/[controller]")]
public class ProductStockController : ControllerBase
Expand Down
52 changes: 22 additions & 30 deletions tests/MiniAuth.Identity/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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<IdentityUser>();
#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<IdentityUser>();
app.MapControllers();
//if (app.Environment.IsDevelopment())
//{
// app.UseSwagger();
// app.UseSwaggerUI();
//}
//app.UseMiniAuth();
app.UseSwagger();
app.UseSwaggerUI();
app.Run();
}
}


}
3 changes: 2 additions & 1 deletion tests/MiniAuth.Identity/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"JWTKey": "40b9e63e-b264-4939-9f12-60a21d96dfbf"
}

0 comments on commit dc44384

Please sign in to comment.