Skip to content

Commit 7b4f8af

Browse files
authored
Merge pull request #23 from damienbod/dev
Update packages, using nullable
2 parents 1c4382d + 156d8bf commit 7b4f8af

16 files changed

+63
-41
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[Readme](https://github.com/damienbod/Blazor.BFF.AzureB2C.Template/blob/main/README.md)
44

5+
**2022-03-20** 1.1.0
6+
- Updated nuget packages
7+
- Using nullable enabled
8+
59
**2022-03-05** 1.0.11
610
- bugfix downstream APIs support
711

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ nuget pack content/Blazor.BFF.AzureB2C.Template.nuspec
9191
Locally built nupkg:
9292

9393
```
94-
dotnet new -i Blazor.BFF.AzureB2C.Template.1.0.11.nupkg
94+
dotnet new -i Blazor.BFF.AzureB2C.Template.1.1.0.nupkg
9595
```
9696

9797
Local folder:

content/Blazor.BFF.AzureB2C.Template.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
33
<metadata>
44
<id>Blazor.BFF.AzureB2C.Template</id>
5-
<version>1.0.11</version>
5+
<version>1.1.0</version>
66
<title>Blazor.BFF.AzureB2C.Template</title>
77
<license type="file">LICENSE</license>
88
<description>Blazor BFF template for WASM ASP.NET Core hosted</description>
@@ -15,7 +15,7 @@
1515
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1616
<copyright>2022 damienbod</copyright>
1717
<summary>This template provides a simple Blazor template with BFF server authentication WASM hosted</summary>
18-
<releaseNotes>bugfix downstream APIs support</releaseNotes>
18+
<releaseNotes>Updated nuget packages, using nullable enabled</releaseNotes>
1919
<repository type="git" url="https://github.com/damienbod/Blazor.BFF.AzureB2C.Template" />
2020
<packageTypes>
2121
<packageType name="Template" />

content/BlazorBffAzureB2C/Client/BlazorBffAzureB2C.Client.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>
9-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.2" />
10-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.2" PrivateAssets="all" />
10+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.3" />
11+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.3" PrivateAssets="all" />
1112
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
12-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.2" />
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="6.0.3" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

content/BlazorBffAzureB2C/Client/Services/AuthorizedHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
2121
{
2222
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
2323
HttpResponseMessage responseMessage;
24-
if (!authState.User.Identity.IsAuthenticated)
24+
if (authState.User.Identity != null && !authState.User.Identity.IsAuthenticated)
2525
{
2626
// if user is not authenticated, immediately set response status to 401 Unauthorized
2727
responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized);

content/BlazorBffAzureB2C/Client/Services/HostAuthenticationStateProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class HostAuthenticationStateProvider : AuthenticationStateProvider
2323
private readonly ILogger<HostAuthenticationStateProvider> _logger;
2424

2525
private DateTimeOffset _userLastCheck = DateTimeOffset.FromUnixTimeSeconds(0);
26-
private ClaimsPrincipal _cachedUser = new ClaimsPrincipal(new ClaimsIdentity());
26+
private ClaimsPrincipal _cachedUser = new(new ClaimsIdentity());
2727

2828
public HostAuthenticationStateProvider(NavigationManager navigation, HttpClient client, ILogger<HostAuthenticationStateProvider> logger)
2929
{
@@ -37,7 +37,7 @@ public override async Task<AuthenticationState> GetAuthenticationStateAsync()
3737
return new AuthenticationState(await GetUser(useCache: true));
3838
}
3939

40-
public void SignIn(string customReturnUrl = null)
40+
public void SignIn(string? customReturnUrl = null)
4141
{
4242
var returnUrl = customReturnUrl != null ? _navigation.ToAbsoluteUri(customReturnUrl).ToString() : null;
4343
var encodedReturnUrl = Uri.EscapeDataString(returnUrl ?? _navigation.Uri);
@@ -63,11 +63,11 @@ private async ValueTask<ClaimsPrincipal> GetUser(bool useCache = false)
6363

6464
private async Task<ClaimsPrincipal> FetchUser()
6565
{
66-
UserInfo user = null;
66+
UserInfo? user = null;
6767

6868
try
6969
{
70-
_logger.LogInformation(_client.BaseAddress.ToString());
70+
_logger.LogInformation("{clientBaseAddress}", _client.BaseAddress?.ToString());
7171
user = await _client.GetFromJsonAsync<UserInfo>("api/User");
7272
}
7373
catch (Exception exc)

content/BlazorBffAzureB2C/Server/BlazorBffAzureB2C.Server.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67

78
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.2" />
9+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.3" />
910
</ItemGroup>
1011

1112
<ItemGroup>
@@ -14,10 +15,10 @@
1415
</ItemGroup>
1516

1617
<ItemGroup>
17-
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.2" NoWarn="NU1605" />
18-
<PackageReference Include="Microsoft.Graph" Version="4.19.0" />
19-
<PackageReference Include="Microsoft.Identity.Web" Version="1.23.0" />
20-
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.23.0" />
18+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.3" NoWarn="NU1605" />
19+
<PackageReference Include="Microsoft.Graph" Version="4.21.0" />
20+
<PackageReference Include="Microsoft.Identity.Web" Version="1.23.1" />
21+
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.23.1" />
2122
<PackageReference Include="IdentityModel.AspNetCore" Version="4.1.3" />
2223
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.16.1" />
2324
</ItemGroup>

content/BlazorBffAzureB2C/Server/Controllers/GraphApiCallsController.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using BlazorBffAzureB2C.Server.Services;
45
using Microsoft.AspNetCore.Authentication.Cookies;
@@ -25,8 +26,15 @@ public GraphApiCallsController(MsGraphService msGraphtService)
2526
[HttpGet]
2627
public async Task<IEnumerable<string>> Get()
2728
{
28-
var userData = await _msGraphtService.GetGraphApiUser(User.GetNameIdentifierId());
29-
return new List<string> { $"DisplayName: {userData.DisplayName}",
29+
var userId = User.GetNameIdentifierId();
30+
if (userId != null)
31+
{
32+
var userData = await _msGraphtService.GetGraphApiUser(userId);
33+
return new List<string> { $"DisplayName: {userData.DisplayName}",
3034
$"GivenName: {userData.GivenName}", $"AboutMe: {userData.AboutMe}" };
35+
}
36+
37+
return Array.Empty<string>();
38+
3139
}
3240
}

content/BlazorBffAzureB2C/Server/Controllers/UserController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ public class UserController : ControllerBase
1717
[AllowAnonymous]
1818
public IActionResult GetCurrentUser()
1919
{
20-
return Ok(User.Identity.IsAuthenticated ? CreateUserInfo(User) : UserInfo.Anonymous);
20+
var userAuthenticated = User.Identity != null && User.Identity.IsAuthenticated;
21+
return Ok(userAuthenticated ? CreateUserInfo(User) : UserInfo.Anonymous);
2122
}
2223

23-
private UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal)
24+
private static UserInfo CreateUserInfo(ClaimsPrincipal claimsPrincipal)
2425
{
25-
if (!claimsPrincipal.Identity.IsAuthenticated)
26+
if (claimsPrincipal.Identity != null && !claimsPrincipal.Identity.IsAuthenticated)
2627
{
2728
return UserInfo.Anonymous;
2829
}

content/BlazorBffAzureB2C/Server/MsGraphClaimsTransformation.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
2323
var objectidentifierClaimType = "http://schemas.microsoft.com/identity/claims/objectidentifier";
2424
var objectIdentifier = principal.Claims.FirstOrDefault(t => t.Type == objectidentifierClaimType);
2525

26-
var groupIds = await _msGraphService.GetGraphApiUserMemberGroups(objectIdentifier.Value);
27-
28-
foreach (var groupId in groupIds.ToList())
26+
if(objectIdentifier != null)
2927
{
30-
claimsIdentity.AddClaim(new Claim(groupClaimType, groupId));
28+
var groupIds = await _msGraphService.GetGraphApiUserMemberGroups(objectIdentifier.Value);
29+
30+
foreach (var groupId in groupIds.ToList())
31+
{
32+
claimsIdentity.AddClaim(new Claim(groupClaimType, groupId));
33+
}
3134
}
3235
}
3336

0 commit comments

Comments
 (0)