Skip to content

Commit bc46398

Browse files
committed
Support .net6,7 frameworks
1 parent 8779f91 commit bc46398

File tree

7 files changed

+75
-24
lines changed

7 files changed

+75
-24
lines changed

README.zh-CN.md

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232

3333
### 简介
3434

35-
一个简单的 ASP.NET Core Identity Web UI 后台管理插件
35+
MiniAuth 一个轻量 ASP.NET Core Identity Web 后台管理插件
3636

37-
「一行代码」为「现有新旧项目」 添加 ASP.NET Core Identity 系统跟用户、权限管理 Web UI
37+
「一行代码」为「新、旧项目」 添加 Identity 系统跟用户、权限管理后台 Web UI
3838

39-
开箱即用,避免需要打掉重写或是严重耦合情况
39+
开箱即用,避免打掉重写或是严重耦合情况
4040

4141
<table>
4242
<tr>
@@ -54,16 +54,22 @@
5454

5555
### 特点
5656

57-
- 简单、拔插设计 : SPA、SSR、API、MVC、Razor Page 开箱即用
57+
- 兼容 : Based on JWT, Cookie, Session 只要符合 .NET identity 规格都能使用。
58+
- 简单 : 拔插设计,API、MVC、Razor Page 等,都能开箱即用
5859
- 多平台 : 支持 linux, macos
59-
- 兼容 : 不对现有系统做侵入式修改,能搭配其他权限框架使用
60-
- 支持多数据库
60+
- 支持多数据库 : 符合 Identity EF Core 规格的数据库都能使用
6161

6262
### 安装
6363

6464
[NuGet](https://www.nuget.org/packages/MiniAuth) 安装套件
6565

66-
### 快速开始 - 视频 :
66+
```
67+
dotnet add package MiniAuth
68+
// or
69+
NuGet\Install-Package MiniAuth
70+
```
71+
72+
6773

6874
#### [视频链接](https://www.bilibili.com/video/BV1ht421n7i9/?share_source=copy_web&vd_source=0f38adeab321d806d9f26c31c53679b7)
6975

@@ -94,29 +100,39 @@
94100

95101
### 已有自己的 identity 情况
96102

97-
把 autoUse 关闭,将 UseMiniAuth 放在自己的 Auth 之后,例子:
103+
AddMiniAuth autoUse 关闭,将 UseMiniAuth 并在泛型参数换上自己的 IdentityDBContext、用户、权限认证,放在自己的 Auth 之后,例子:
98104
```csharp
99105
public static void Main(string[] args)
100106
{
101107
var builder = WebApplication.CreateBuilder(args);
102108

103-
builder.Services.AddMiniAuth(autoUse:false);
109+
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
110+
builder.Services.AddDbContext<ApplicationDbContext>(options =>
111+
options.UseSqlServer(connectionString));
112+
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
104113

105-
builder.Services.AddControllers();
114+
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
115+
.AddRoles<IdentityRole>()
116+
.AddEntityFrameworkStores<ApplicationDbContext>();
106117

107-
var app = builder.Build();
118+
builder.Services.AddControllersWithViews();
108119

109-
app.UseAuthentication();
110-
app.UseMiniAuth(); //
120+
builder.Services.AddMiniAuth(autoUse: false); // <= ❗❗❗
111121
112-
app.MapControllers();
113122

123+
var app = builder.Build();
124+
125+
app.UseMiniAuth<ApplicationDbContext, IdentityUser, IdentityRole>(); // <= ❗❗❗
126+
app.MapControllerRoute(
127+
name: "default",
128+
pattern: "{controller=Home}/{action=Index}/{id?}");
129+
app.MapRazorPages();
114130

115131
app.Run();
116132
}
117133
```
118134

119-
#### 注意1
135+
#### 注意顺序
120136
请将 UseMiniAuth 放在路由生成之后,否则系统无法获取路由数据作权限判断,如 :
121137

122138
```c#
@@ -135,9 +151,8 @@ builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.Requ
135151

136152
### 更换数据库
137153

138-
#### SQLite
139-
140-
系统预设使用 SQLite,无需做任何设定代码。
154+
MiniAuth 系统预设使用 SQLite,无需做任何设定代码
155+
如果需要切换请在 `app.UseMiniAuth` 泛型指定不同的数据库型别。
141156

142157

143158
### 设定、选项
@@ -147,14 +162,14 @@ builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.Requ
147162
- MiniAuth 预设模式为IT Admin 集中用户管理,用户注册、密码重置等操作需要 Admin 权限账号操作,预设 Role = miniauth-admin
148163

149164
#### 登录、用户验证
165+
150166
非 ApiController 预设登录导向 login.html 页面 (判断方式Headers["X-Requested-With"] == "XMLHttpRequest" 或是 ApiControllerAttribute)
151167
ApiController 的 Controller 预设不会导向登录页面,而是返回 401 status code
152168

153169

154170
### 分布式系统
155171

156-
- 数据库来源请换成 SQL Server、MySQL、PostgreSQL 等数据库,系统预设使用 SQLite
157-
- 请确认每个机器上的 `miniauth.pfx`, `miniauthsalt.cer`使用同一份,否则会导致验证失败。
172+
- 数据库来源请换成 SQL Server、MySQL、PostgreSQL 等数据库
158173

159174

160175
### 更新日志

src/MiniAuth.IdentityAuth/MiniAuth.IdentityAuth.csproj

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@
22

33
<PropertyGroup>
44
<AssemblyName>MiniAuth</AssemblyName>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
1111
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
12-
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1312
</ItemGroup>
13+
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0" />
15+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.0" />
16+
</ItemGroup>
17+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
19+
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.0" />
20+
</ItemGroup>
21+
<ItemGroup>
22+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
23+
</ItemGroup>
1424
<ItemGroup>
1525
<EmbeddedResource Include="wwwroot/**/*" Exclude="**/*/*.map;**/*/*.json;**/*/*.md" />
1626
</ItemGroup>

src/MiniAuth.IdentityAuth/MiniAuthIdentityBuilderExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
using Microsoft.EntityFrameworkCore;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.FileProviders;
7+
using System;
78
using System.Data;
89
using System.Diagnostics;
10+
using System.Linq;
911
using System.Reflection;
1012
using System.Security.Principal;
1113
using System.Text.Json;
14+
using System.Threading;
15+
using System.Threading.Tasks;
1216

1317

1418
public static class MiniAuthIdentityBuilderExtensions

src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
using Microsoft.Extensions.Options;
1212
using MiniAuth.IdentityAuth.Helpers;
1313
using MiniAuth.IdentityAuth.Models;
14+
using System;
1415
using System.Collections.Concurrent;
16+
using System.IO;
17+
using System.Linq;
1518
using System.Security.Claims;
1619
using System.Text;
1720
using System.Text.Json;
21+
using System.Threading.Tasks;
1822

1923

2024
internal class MiniAuthIdentityEndpoints<TDbContext, TIdentityUser, TIdentityRole>

src/MiniAuth.IdentityAuth/MiniAuthIdentityMiddleware.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
using Microsoft.Extensions.Logging;
88
using Microsoft.Extensions.Options;
99
using MiniAuth.Identity;
10+
using System;
1011
using System.Diagnostics;
1112
using System.Reflection;
13+
using System.Threading.Tasks;
1214

1315

1416
public partial class MiniAuthIdentityMiddleware

src/MiniAuth.IdentityAuth/MiniAuthIdentityServiceExtensions.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.Extensions.Options;
1212
using MiniAuth.Identity;
13+
using System;
1314
using System.Diagnostics;
1415
using System.Diagnostics.CodeAnalysis;
16+
using System.Linq;
1517
using System.Resources;
1618
using System.Text.Json;
19+
using System.Threading.Tasks;
1720

1821

1922
public static class MiniAuthIdentityServiceExtensions
@@ -83,12 +86,20 @@ public static IServiceCollection AddMiniAuth<TDbContext, TIdentityUser, TIdentit
8386
return services;
8487
}
8588

86-
public static IdentityBuilder AddMiniAuth<TUser, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TRole>(
89+
public static IdentityBuilder AddMiniAuth<TUser,
90+
#if NET8_0_OR_GREATER
91+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
92+
#endif
93+
TRole>(
8794
this IServiceCollection services)
8895
where TUser : class
8996
where TRole : class
9097
=> services.AddMiniAuthIdentity<TUser, TRole>(setupAction: null!);
91-
public static IdentityBuilder AddMiniAuthIdentity<TUser, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TRole>(
98+
public static IdentityBuilder AddMiniAuthIdentity<TUser,
99+
#if NET8_0_OR_GREATER
100+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
101+
#endif
102+
TRole>(
92103
this IServiceCollection services,
93104
Action<IdentityOptions> setupAction)
94105
where TUser : class
@@ -171,7 +182,9 @@ public static IServiceCollection AddMiniAuth<TDbContext, TIdentityUser, TIdentit
171182
// No interface for the error describer so we can add errors without rev'ing the interface
172183
services.TryAddScoped<IdentityErrorDescriber>();
173184
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
185+
#if NET8_0_OR_GREATER
174186
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<SecurityStampValidatorOptions>, PostConfigureSecurityStampValidatorOptions>());
187+
#endif
175188
services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
176189
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
177190
services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
@@ -208,6 +221,7 @@ private static OptionsBuilder<CookieAuthenticationOptions> AddMiniAuthApplicatio
208221
});
209222
return new OptionsBuilder<CookieAuthenticationOptions>(builder.Services, IdentityConstants.ApplicationScheme);
210223
}
224+
#if NET8_0_OR_GREATER
211225
private sealed class PostConfigureSecurityStampValidatorOptions : IPostConfigureOptions<SecurityStampValidatorOptions>
212226
{
213227
public PostConfigureSecurityStampValidatorOptions(TimeProvider timeProvider)
@@ -222,5 +236,6 @@ public void PostConfigure(string? name, SecurityStampValidatorOptions options)
222236
options.TimeProvider ??= TimeProvider;
223237
}
224238
}
239+
#endif
225240
}
226241

src/MiniAuth.IdentityAuth/MiniAuthStartupFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.AspNetCore.Identity;
4+
using System;
45
using System.Diagnostics;
56

67
namespace MiniAuth.Identity

0 commit comments

Comments
 (0)