Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Identity): Support Blazor Server、Support Blazor Assembly #230

Merged
merged 10 commits into from
Sep 8, 2022
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<MicrosoftTeskSdkPackageVersion>16.9.4</MicrosoftTeskSdkPackageVersion>
<CoverletPackageVersion>3.0.2</CoverletPackageVersion>
<MoqPackageVersion>4.16.1</MoqPackageVersion>
<NSubstitutePackageVersion>4.4.0</NSubstitutePackageVersion>
<MapsterPackageVersion>7.3.0</MapsterPackageVersion>
<DaprPackageVersion>1.5.0</DaprPackageVersion>
<GoogleProtobufPackageVersion>3.19.1</GoogleProtobufPackageVersion>
Expand Down
55 changes: 55 additions & 0 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.Serializa
EndProject
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}"
EndProject
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}"
EndProject
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}"
EndProject
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}"
EndProject
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}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1897,6 +1907,46 @@ Global
{4FF4632A-4E93-40BF-8D01-843548560F76}.Release|Any CPU.Build.0 = Release|Any CPU
{4FF4632A-4E93-40BF-8D01-843548560F76}.Release|x64.ActiveCfg = Release|Any CPU
{4FF4632A-4E93-40BF-8D01-843548560F76}.Release|x64.Build.0 = Release|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|x64.ActiveCfg = Debug|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Debug|x64.Build.0 = Debug|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|Any CPU.Build.0 = Release|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|x64.ActiveCfg = Release|Any CPU
{FC5C0C14-236E-4709-8BCD-541197075426}.Release|x64.Build.0 = Release|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|x64.ActiveCfg = Debug|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Debug|x64.Build.0 = Debug|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|Any CPU.Build.0 = Release|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|x64.ActiveCfg = Release|Any CPU
{77DC7576-25E6-49B4-B49E-34B59E484A2E}.Release|x64.Build.0 = Release|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|x64.ActiveCfg = Debug|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Debug|x64.Build.0 = Debug|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|Any CPU.Build.0 = Release|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|x64.ActiveCfg = Release|Any CPU
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87}.Release|x64.Build.0 = Release|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|x64.ActiveCfg = Debug|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Debug|x64.Build.0 = Debug|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|Any CPU.Build.0 = Release|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|x64.ActiveCfg = Release|Any CPU
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3}.Release|x64.Build.0 = Release|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|x64.ActiveCfg = Debug|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Debug|x64.Build.0 = Debug|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|Any CPU.Build.0 = Release|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|x64.ActiveCfg = Release|Any CPU
{60A02980-398C-40CD-9FAA-ACAD7A9A866C}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2161,6 +2211,11 @@ Global
{C55B75D2-43E0-4D3D-B705-110C4255528A} = {6E93CADC-B6C7-4063-BEA7-56068D682C31}
{A86DA6FB-58E0-4FEE-B663-C336F9B3A3A5} = {6E93CADC-B6C7-4063-BEA7-56068D682C31}
{4FF4632A-4E93-40BF-8D01-843548560F76} = {FB0F5EB7-AA72-4D54-BD8D-43B173E54E19}
{FC5C0C14-236E-4709-8BCD-541197075426} = {3198630F-C09B-40AE-B9A1-E79249296E48}
{77DC7576-25E6-49B4-B49E-34B59E484A2E} = {3198630F-C09B-40AE-B9A1-E79249296E48}
{9282075C-9C15-409B-BDC4-5EC2CB9EFA87} = {3198630F-C09B-40AE-B9A1-E79249296E48}
{583ECD6A-5960-4A60-833A-DB6DD93BE9C3} = {94D15C26-7204-4299-BC23-B89F5A0B0BF9}
{60A02980-398C-40CD-9FAA-ACAD7A9A866C} = {94D15C26-7204-4299-BC23-B89F5A0B0BF9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Authentication.Identity.BlazorAssembly;

public class BlazorCurrentPrincipalAccessor : ICurrentPrincipalAccessor
{
readonly AuthenticationStateProvider _authenticationStateProvider;

public BlazorCurrentPrincipalAccessor(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
}

public ClaimsPrincipal? GetCurrentPrincipal()
{
return _authenticationStateProvider.GetAuthenticationStateAsync().Result.User;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Masa.Contrib.Authentication.Identity.Core\Masa.Contrib.Authentication.Identity.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[中](README.zh-CN.md) | EN

## Authentication.Identity.BlazorAssembly

Provides `Identity` implementation for projects, supports `Blazor Assembly` projects

Example:

``` C#
Install-Package Masa.Contrib.Authentication.Identity.BlazorAssembly
```

1. Modify `Program.cs`

``` C#
builder.Services.AddMasaIdentity();
```

2. Get user information

``` C#
IUserContext userContext;//Get IUserContext from DI
userContext.User;//Get user information
```

3. Temporarily change the current login user information

``` C#
IUserSetter userSetter;//Get IUserSetter from DI
var user = new IdentityUser()
{
Id = "2",
UserName = "Tom",
Environment = "Production",
TenantId = "2"
};
using(userSetter.Change(user))
{
//The obtained user information is Tom
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
中 | [EN](README.md)

## Authentication.Identity.BlazorAssembly

为项目提供了`Identity`实现,支持`Blazor Assembly`项目

用例:

``` C#
Install-Package Masa.Contrib.Authentication.Identity.BlazorAssembly
```

1. 修改`Program.cs`

``` C#
builder.Services.AddMasaIdentity();
```

2. 获取用户信息

``` C#
IUserContext userContext;//从DI中获取IUserContext
userContext.User;//获取用户信息
```

3. 临时更改当前登录用户信息

``` C#
IUserSetter userSetter;//从DI中获取IUserSetter
var user = new IdentityUser()
{
Id = "2",
UserName = "Tom",
Environment = "Production",
TenantId = "2"
};
using (userSetter.Change(user))
{
//获取到的用户信息为Tom
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMasaIdentity(
this IServiceCollection services)
=> AddMasaIdentityCore(services).AddMasaIdentityCore();

public static IServiceCollection AddMasaIdentity(
this IServiceCollection services,
Action<IdentityClaimOptions> configureOptions)
=> AddMasaIdentityCore(services).AddMasaIdentityCore(configureOptions);

public static IServiceCollection AddMasaIdentity(
this IServiceCollection services,
string serializationName)
=> AddMasaIdentityCore(services).AddMasaIdentityCore(serializationName);

public static IServiceCollection AddMasaIdentity(
this IServiceCollection services,
string serializationName,
Action<IdentityClaimOptions> configureOptions)
=> AddMasaIdentityCore(services).AddMasaIdentityCore(serializationName, configureOptions);

private static IServiceCollection AddMasaIdentityCore(IServiceCollection services)
{
services.AddAuthorizationCore();
services.TryAddScoped<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.Contrib.Authentication.Identity;
global using Masa.Contrib.Authentication.Identity.BlazorAssembly;
global using Microsoft.AspNetCore.Components.Authorization;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using System.Security.Claims;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Authentication.Identity.BlazorServer;

public class BlazorCurrentPrincipalAccessor : ICurrentPrincipalAccessor
{
readonly AuthenticationStateProvider _authenticationStateProvider;

public BlazorCurrentPrincipalAccessor(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
}

public ClaimsPrincipal? GetCurrentPrincipal()
{
return _authenticationStateProvider.GetAuthenticationStateAsync().ConfigureAwait(false).GetAwaiter().GetResult().User;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Masa.Contrib.Authentication.Identity.Core\Masa.Contrib.Authentication.Identity.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[中](README.zh-CN.md) | EN

## Authentication.Identity.BlazorServer

Provides an `Identity` implementation for the project, supports the `Blazor Server` project

Example:

``` C#
Install-Package Masa.Contrib.Authentication.Identity.BlazorServer
```

1. Modify `Program.cs`

``` C#
builder.Services.AddMasaIdentity();
```

2. Get user information

``` C#
IUserContext userContext;//Get IUserContext from DI
userContext.User;//Get user information
```

3. Temporarily change the current login user information

``` C#
IUserSetter userSetter;//Get IUserSetter from DI
var user = new IdentityUser()
{
Id = "2",
UserName = "Tom",
Environment = "Production",
TenantId = "2"
};
using(userSetter.Change(user))
{
//The obtained user information is Tom
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
中 | [EN](README.md)

## Authentication.Identity.BlazorServer

为项目提供了`Identity`实现,支持`Blazor Server`项目

用例:

``` C#
Install-Package Masa.Contrib.Authentication.Identity.BlazorServer
```

1. 修改`Program.cs`

``` C#
builder.Services.AddMasaIdentity();
```

2. 获取用户信息

``` C#
IUserContext userContext;//从DI中获取IUserContext
userContext.User;//获取用户信息
```

3. 临时更改当前登录用户信息

``` C#
IUserSetter userSetter;//从DI中获取IUserSetter
var user = new IdentityUser()
{
Id = "2",
UserName = "Tom",
Environment = "Production",
TenantId = "2"
};
using (userSetter.Change(user))
{
//获取到的用户信息为Tom
}
```
Loading