Skip to content

Commit 3b64737

Browse files
authored
feat(Identity): Support Blazor Server、Support Blazor Assembly (#230)
* feat(Identity): Support Blazor Server、Support Blazor Assembly * chore: Remove invalid references * chore: deal with bad taste * chore: Ignore invalid checks * chore: Remove invalid tags * doc: Modify the document * doc: Modify README.md
1 parent 998f3f3 commit 3b64737

File tree

45 files changed

+1068
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1068
-98
lines changed

Directory.Build.props

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<MicrosoftTeskSdkPackageVersion>16.9.4</MicrosoftTeskSdkPackageVersion>
77
<CoverletPackageVersion>3.0.2</CoverletPackageVersion>
88
<MoqPackageVersion>4.16.1</MoqPackageVersion>
9+
<NSubstitutePackageVersion>4.4.0</NSubstitutePackageVersion>
910
<MapsterPackageVersion>7.3.0</MapsterPackageVersion>
1011
<DaprPackageVersion>1.5.0</DaprPackageVersion>
1112
<GoogleProtobufPackageVersion>3.19.1</GoogleProtobufPackageVersion>

Masa.Framework.sln

+55
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.Serializa
537537
EndProject
538538
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.BuildingBlocks.ReadWriteSpliting.EventSourcing", "src\BuildingBlocks\ReadWriteSplitting\Masa.BuildingBlocks.ReadWriteSplitting.EventSourcing\Masa.BuildingBlocks.ReadWriteSpliting.EventSourcing.csproj", "{4FF4632A-4E93-40BF-8D01-843548560F76}"
539539
EndProject
540+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Authentication.Identity.Core", "src\Contrib\Authentication\Masa.Contrib.Authentication.Identity.Core\Masa.Contrib.Authentication.Identity.Core.csproj", "{FC5C0C14-236E-4709-8BCD-541197075426}"
541+
EndProject
542+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Authentication.Identity.BlazorServer", "src\Contrib\Authentication\Masa.Contrib.Authentication.Identity.BlazorServer\Masa.Contrib.Authentication.Identity.BlazorServer.csproj", "{77DC7576-25E6-49B4-B49E-34B59E484A2E}"
543+
EndProject
544+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Authentication.Identity.BlazorAssembly", "src\Contrib\Authentication\Masa.Contrib.Authentication.Identity.BlazorAssembly\Masa.Contrib.Authentication.Identity.BlazorAssembly.csproj", "{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}"
545+
EndProject
546+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Authentication.Identity.BlazorServer.Tests", "src\Contrib\Authentication\Tests\Masa.Contrib.Authentication.Identity.BlazorServer.Tests\Masa.Contrib.Authentication.Identity.BlazorServer.Tests.csproj", "{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}"
547+
EndProject
548+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Authentication.Identity.BlazorAssembly.Tests", "src\Contrib\Authentication\Tests\Masa.Contrib.Authentication.Identity.BlazorAssembly.Tests\Masa.Contrib.Authentication.Identity.BlazorAssembly.Tests.csproj", "{60A02980-398C-40CD-9FAA-ACAD7A9A866C}"
549+
EndProject
540550
Global
541551
GlobalSection(SolutionConfigurationPlatforms) = preSolution
542552
Debug|Any CPU = Debug|Any CPU
@@ -1897,6 +1907,46 @@ Global
18971907
{4FF4632A-4E93-40BF-8D01-843548560F76}.Release|Any CPU.Build.0 = Release|Any CPU
18981908
{4FF4632A-4E93-40BF-8D01-843548560F76}.Release|x64.ActiveCfg = Release|Any CPU
18991909
{4FF4632A-4E93-40BF-8D01-843548560F76}.Release|x64.Build.0 = Release|Any CPU
1910+
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1911+
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|Any CPU.Build.0 = Debug|Any CPU
1912+
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|x64.ActiveCfg = Debug|Any CPU
1913+
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|x64.Build.0 = Debug|Any CPU
1914+
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|Any CPU.ActiveCfg = Release|Any CPU
1915+
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|Any CPU.Build.0 = Release|Any CPU
1916+
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|x64.ActiveCfg = Release|Any CPU
1917+
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|x64.Build.0 = Release|Any CPU
1918+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1919+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1920+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|x64.ActiveCfg = Debug|Any CPU
1921+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|x64.Build.0 = Debug|Any CPU
1922+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1923+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|Any CPU.Build.0 = Release|Any CPU
1924+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|x64.ActiveCfg = Release|Any CPU
1925+
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|x64.Build.0 = Release|Any CPU
1926+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1927+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|Any CPU.Build.0 = Debug|Any CPU
1928+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|x64.ActiveCfg = Debug|Any CPU
1929+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|x64.Build.0 = Debug|Any CPU
1930+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|Any CPU.ActiveCfg = Release|Any CPU
1931+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|Any CPU.Build.0 = Release|Any CPU
1932+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|x64.ActiveCfg = Release|Any CPU
1933+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|x64.Build.0 = Release|Any CPU
1934+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1935+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
1936+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|x64.ActiveCfg = Debug|Any CPU
1937+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|x64.Build.0 = Debug|Any CPU
1938+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
1939+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|Any CPU.Build.0 = Release|Any CPU
1940+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|x64.ActiveCfg = Release|Any CPU
1941+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|x64.Build.0 = Release|Any CPU
1942+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1943+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|Any CPU.Build.0 = Debug|Any CPU
1944+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|x64.ActiveCfg = Debug|Any CPU
1945+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|x64.Build.0 = Debug|Any CPU
1946+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|Any CPU.ActiveCfg = Release|Any CPU
1947+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|Any CPU.Build.0 = Release|Any CPU
1948+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|x64.ActiveCfg = Release|Any CPU
1949+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|x64.Build.0 = Release|Any CPU
19001950
EndGlobalSection
19011951
GlobalSection(SolutionProperties) = preSolution
19021952
HideSolutionNode = FALSE
@@ -2161,6 +2211,11 @@ Global
21612211
{C55B75D2-43E0-4D3D-B705-110C4255528A} = {6E93CADC-B6C7-4063-BEA7-56068D682C31}
21622212
{A86DA6FB-58E0-4FEE-B663-C336F9B3A3A5} = {6E93CADC-B6C7-4063-BEA7-56068D682C31}
21632213
{4FF4632A-4E93-40BF-8D01-843548560F76} = {FB0F5EB7-AA72-4D54-BD8D-43B173E54E19}
2214+
{FC5C0C14-236E-4709-8BCD-541197075426} = {3198630F-C09B-40AE-B9A1-E79249296E48}
2215+
{77DC7576-25E6-49B4-B49E-34B59E484A2E} = {3198630F-C09B-40AE-B9A1-E79249296E48}
2216+
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87} = {3198630F-C09B-40AE-B9A1-E79249296E48}
2217+
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3} = {94D15C26-7204-4299-BC23-B89F5A0B0BF9}
2218+
{60A02980-398C-40CD-9FAA-ACAD7A9A866C} = {94D15C26-7204-4299-BC23-B89F5A0B0BF9}
21642219
EndGlobalSection
21652220
GlobalSection(ExtensibilityGlobals) = postSolution
21662221
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Authentication.Identity.BlazorAssembly;
5+
6+
public class BlazorCurrentPrincipalAccessor : ICurrentPrincipalAccessor
7+
{
8+
readonly AuthenticationStateProvider _authenticationStateProvider;
9+
10+
public BlazorCurrentPrincipalAccessor(AuthenticationStateProvider authenticationStateProvider)
11+
{
12+
_authenticationStateProvider = authenticationStateProvider;
13+
}
14+
15+
public ClaimsPrincipal? GetCurrentPrincipal()
16+
{
17+
return _authenticationStateProvider.GetAuthenticationStateAsync().Result.User;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(MicrosoftPackageVersion)" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\Masa.Contrib.Authentication.Identity.Core\Masa.Contrib.Authentication.Identity.Core.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[](README.zh-CN.md) | EN
2+
3+
## Authentication.Identity.BlazorAssembly
4+
5+
Provides `Identity` implementation, supports `Blazor Assembly` projects
6+
7+
Example:
8+
9+
``` C#
10+
Install-Package Masa.Contrib.Authentication.Identity.BlazorAssembly
11+
```
12+
13+
1. Modify `Program.cs`
14+
15+
``` C#
16+
builder.Services.AddMasaIdentity();
17+
```
18+
19+
2. Get user information
20+
21+
``` C#
22+
IUserContext userContext;//Get IUserContext from DI
23+
userContext.User;//Get user information
24+
```
25+
26+
3. Temporarily change the current login user information
27+
28+
``` C#
29+
IUserSetter userSetter;//Get IUserSetter from DI
30+
var user = new IdentityUser()
31+
{
32+
Id = "2",
33+
UserName = "Tom",
34+
Environment = "Production",
35+
TenantId = "2"
36+
};
37+
using(userSetter.Change(user))
38+
{
39+
//The obtained user information is Tom
40+
}
41+
//The user information obtained again is the original logged in user
42+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
中 | [EN](README.md)
2+
3+
## Authentication.Identity.BlazorAssembly
4+
5+
提供了`Identity`实现,支持`Blazor Assembly`项目
6+
7+
用例:
8+
9+
``` C#
10+
Install-Package Masa.Contrib.Authentication.Identity.BlazorAssembly
11+
```
12+
13+
1. 修改`Program.cs`
14+
15+
``` C#
16+
builder.Services.AddMasaIdentity();
17+
```
18+
19+
2. 获取用户信息
20+
21+
``` C#
22+
IUserContext userContext;//从DI中获取IUserContext
23+
userContext.User;//获取用户信息
24+
```
25+
26+
3. 临时更改当前登录用户信息
27+
28+
``` C#
29+
IUserSetter userSetter;//从DI中获取IUserSetter
30+
var user = new IdentityUser()
31+
{
32+
Id = "2",
33+
UserName = "Tom",
34+
Environment = "Production",
35+
TenantId = "2"
36+
};
37+
using (userSetter.Change(user))
38+
{
39+
//获取到的用户信息为Tom
40+
}
41+
//再次获取到的用户信息为原始登录用户
42+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Microsoft.Extensions.DependencyInjection;
5+
6+
public static class ServiceCollectionExtensions
7+
{
8+
public static IServiceCollection AddMasaIdentity(
9+
this IServiceCollection services)
10+
=> AddMasaIdentityCore(services).AddMasaIdentityCore();
11+
12+
public static IServiceCollection AddMasaIdentity(
13+
this IServiceCollection services,
14+
Action<IdentityClaimOptions> configureOptions)
15+
=> AddMasaIdentityCore(services).AddMasaIdentityCore(configureOptions);
16+
17+
public static IServiceCollection AddMasaIdentity(
18+
this IServiceCollection services,
19+
string serializationName)
20+
=> AddMasaIdentityCore(services).AddMasaIdentityCore(serializationName);
21+
22+
public static IServiceCollection AddMasaIdentity(
23+
this IServiceCollection services,
24+
string serializationName,
25+
Action<IdentityClaimOptions> configureOptions)
26+
=> AddMasaIdentityCore(services).AddMasaIdentityCore(serializationName, configureOptions);
27+
28+
private static IServiceCollection AddMasaIdentityCore(IServiceCollection services)
29+
{
30+
services.AddAuthorizationCore();
31+
services.TryAddScoped<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>();
32+
return services;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
global using Masa.Contrib.Authentication.Identity;
5+
global using Masa.Contrib.Authentication.Identity.BlazorAssembly;
6+
global using Microsoft.AspNetCore.Components.Authorization;
7+
global using Microsoft.Extensions.DependencyInjection.Extensions;
8+
global using System.Security.Claims;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Authentication.Identity.BlazorServer;
5+
6+
public class BlazorCurrentPrincipalAccessor : ICurrentPrincipalAccessor
7+
{
8+
readonly AuthenticationStateProvider _authenticationStateProvider;
9+
10+
public BlazorCurrentPrincipalAccessor(AuthenticationStateProvider authenticationStateProvider)
11+
{
12+
_authenticationStateProvider = authenticationStateProvider;
13+
}
14+
15+
public ClaimsPrincipal? GetCurrentPrincipal()
16+
{
17+
return _authenticationStateProvider.GetAuthenticationStateAsync().ConfigureAwait(false).GetAwaiter().GetResult().User;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(MicrosoftPackageVersion)" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\Masa.Contrib.Authentication.Identity.Core\Masa.Contrib.Authentication.Identity.Core.csproj" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[](README.zh-CN.md) | EN
2+
3+
## Authentication.Identity.BlazorServer
4+
5+
Provides an `Identity` implementation for the project, supports the `Blazor Server` project
6+
7+
Example:
8+
9+
``` C#
10+
Install-Package Masa.Contrib.Authentication.Identity.BlazorServer
11+
```
12+
13+
1. Modify `Program.cs`
14+
15+
``` C#
16+
builder.Services.AddMasaIdentity();
17+
```
18+
19+
2. Get user information
20+
21+
``` C#
22+
IUserContext userContext;//Get IUserContext from DI
23+
userContext.User;//Get user information
24+
```
25+
26+
3. Temporarily change the current login user information
27+
28+
``` C#
29+
IUserSetter userSetter;//Get IUserSetter from DI
30+
var user = new IdentityUser()
31+
{
32+
Id = "2",
33+
UserName = "Tom",
34+
Environment = "Production",
35+
TenantId = "2"
36+
};
37+
using(userSetter.Change(user))
38+
{
39+
//The obtained user information is Tom
40+
}
41+
//The user information obtained again is the original logged in user
42+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
中 | [EN](README.md)
2+
3+
## Authentication.Identity.BlazorServer
4+
5+
为项目提供了`Identity`实现,支持`Blazor Server`项目
6+
7+
用例:
8+
9+
``` C#
10+
Install-Package Masa.Contrib.Authentication.Identity.BlazorServer
11+
```
12+
13+
1. 修改`Program.cs`
14+
15+
``` C#
16+
builder.Services.AddMasaIdentity();
17+
```
18+
19+
2. 获取用户信息
20+
21+
``` C#
22+
IUserContext userContext;//从DI中获取IUserContext
23+
userContext.User;//获取用户信息
24+
```
25+
26+
3. 临时更改当前登录用户信息
27+
28+
``` C#
29+
IUserSetter userSetter;//从DI中获取IUserSetter
30+
var user = new IdentityUser()
31+
{
32+
Id = "2",
33+
UserName = "Tom",
34+
Environment = "Production",
35+
TenantId = "2"
36+
};
37+
using (userSetter.Change(user))
38+
{
39+
//获取到的用户信息为Tom
40+
}
41+
//再次获取到的用户信息为原始登录用户
42+
```

0 commit comments

Comments
 (0)