Skip to content

Commit

Permalink
Merge pull request #32 from damienbod/dev-updates
Browse files Browse the repository at this point in the history
Graph SDK 5 support, updated packages
  • Loading branch information
damienbod authored Jun 22, 2023
2 parents a7b0e2b + 7c71071 commit 660f61c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

**2023-06-21** 2.1.0
- Switched to Graph SDK 5
- Updated packages
- fixed Graph photo encoding

**2023-03-11** 2.0.2
- Fix security headers, use on app requests including API calls
- Updated packages
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ nuget pack content/Blazor.BFF.AzureAD.Template.nuspec
Locally built nupkg:

```
dotnet new -i Blazor.BFF.AzureAD.Template.2.0.2.nupkg
dotnet new -i Blazor.BFF.AzureAD.Template.2.1.0.nupkg
```

Local folder:
Expand Down
4 changes: 2 additions & 2 deletions content/Blazor.BFF.AzureAD.Template.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Blazor.BFF.AzureAD.Template</id>
<version>2.0.2</version>
<version>2.1.0</version>
<title>Blazor.BFF.AzureAD.Template</title>
<license type="file">LICENSE</license>
<description>Blazor backend for frontend (BFF) template for WASM ASP.NET Core hosted</description>
Expand All @@ -15,7 +15,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<copyright>2023 damienbod</copyright>
<summary>This template provides a simple Blazor template with BFF server authentication WASM hosted</summary>
<releaseNotes>Updated nuget packages, fixed security headers</releaseNotes>
<releaseNotes>Updated nuget packages, switch to Graph SDK5</releaseNotes>
<repository type="git" url="https://github.com/damienbod/Blazor.BFF.AzureAD.Template" />
<packageTypes>
<packageType name="Template" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.7" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="7.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 6 additions & 9 deletions content/BlazorBffAzureAD/Server/BlazorBffAzureAD.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Client\BlazorBffAzureAD.Client.csproj" />
<ProjectReference Include="..\Shared\BlazorBffAzureAD.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.3" NoWarn="NU1605" />
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="2.5.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.5.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.5.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.18.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.7" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.7" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.12.4" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.12.4" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.12.4" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.19.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public GraphApiCallsController(MsGraphService graphApiClientService)
public async Task<IEnumerable<string>> Get()
{
var userData = await _graphApiClientService.GetGraphApiUser();
if (userData == null)
return new List<string> { "no user data" };

return new List<string> { $"DisplayName: {userData.DisplayName}",
$"GivenName: {userData.GivenName}", $"AboutMe: {userData.AboutMe}" };
}
Expand Down
2 changes: 1 addition & 1 deletion content/BlazorBffAzureAD/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

services.AddMicrosoftIdentityWebAppAuthentication(configuration)
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph("https://graph.microsoft.com/v1.0", scopes)
.AddMicrosoftGraph("https://graph.microsoft.com/v1.0", initialScopes)
.AddInMemoryTokenCaches();

services.AddControllersWithViews(options =>
Expand Down
21 changes: 11 additions & 10 deletions content/BlazorBffAzureAD/Server/Services/MsGraphService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Identity.Web;
using Microsoft.IdentityModel.Tokens;

namespace BlazorBffAzureAD.Server.Services
{
Expand All @@ -12,13 +14,10 @@ public MsGraphService(GraphServiceClient graphServiceClient)
_graphServiceClient = graphServiceClient;
}

public async Task<User> GetGraphApiUser()
public async Task<User?> GetGraphApiUser()
{
return await _graphServiceClient
.Me
.Request()
.WithScopes("User.ReadBasic.All", "user.read")
.GetAsync();
return await _graphServiceClient.Me
.GetAsync(b => b.Options.WithScopes("User.ReadBasic.All", "user.read"));
}

public async Task<string> GetGraphApiProfilePhoto()
Expand All @@ -28,11 +27,13 @@ public async Task<string> GetGraphApiProfilePhoto()
var photo = string.Empty;
// Get user photo
using (var photoStream = await _graphServiceClient
.Me.Photo.Content.Request()
.WithScopes("User.ReadBasic.All", "user.read").GetAsync())
.Me
.Photo
.Content
.GetAsync(b => b.Options.WithScopes("User.ReadBasic.All", "user.read")))
{
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
photo = Convert.ToBase64String(photoByte);
byte[] photoByte = ((MemoryStream)photoStream!).ToArray();
photo = Base64UrlEncoder.Encode(photoByte);
}

return photo;
Expand Down

0 comments on commit 660f61c

Please sign in to comment.