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

.NET 8 #33

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-12-07** 3.0.0
- Updated .NET 8
- Fixed Graph SDK photo streaming
- Rebranding Microsoft Entra ID

**2023-11-03** 2.2.0
- Updated packages
- fixed XSS security header
Expand Down
12 changes: 6 additions & 6 deletions README-NUGET.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[![.NET](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/actions/workflows/dotnet.yml/badge.svg)](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/actions/workflows/dotnet.yml) [![NuGet Status](http://img.shields.io/nuget/v/Blazor.BFF.AzureAD.Template.svg?style=flat-square)](https://www.nuget.org/packages/Blazor.BFF.AzureAD.Template/) [Change log](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/blob/main/Changelog.md)

This template can be used to create a Blazor WASM application hosted in an ASP.NET Core Web app using Azure AD and Microsoft.Identity.Web to authenticate using the BFF security architecture. (server authentication) This removes the tokens from the browser and uses cookies with each HTTP request, response. The template also adds the required security headers as best it can for a Blazor application.
This template can be used to create a Blazor WASM application hosted in an ASP.NET Core Web app using Microsoft Entra ID and Microsoft.Identity.Web to authenticate using the BFF security architecture. (server authentication) This removes the tokens from the browser and uses cookies with each HTTP request, response. The template also adds the required security headers as best it can for a Blazor application.

## Features

- WASM hosted in ASP.NET Core 7
- BFF with Azure AD using Microsoft.Identity.Web
- WASM hosted in ASP.NET Core 8
- BFF with Microsoft Entra ID using Microsoft.Identity.Web
- OAuth2 and OpenID Connect OIDC
- No tokens in the browser
- Azure AD Continuous Access Evaluation CAE support
- Microsoft Entra ID Continuous Access Evaluation CAE support

## Using the template

Expand All @@ -30,7 +30,7 @@ Use the `-n` or `--name` parameter to change the name of the output created. Thi

## Setup after installation

Add the Azure AD App registration settings
Add the Microsoft Entra ID App registration settings

```
{
Expand Down Expand Up @@ -171,7 +171,7 @@ public IActionResult Get()
dotnet new uninstall Blazor.BFF.AzureAD.Template
```

## Credits, Used NuGet packages + ASP.NET Core 7.0 standard packages
## Credits, Used NuGet packages + ASP.NET Core 8.0 standard packages

- NetEscapades.AspNetCore.SecurityHeaders

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

[![.NET](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/actions/workflows/dotnet.yml/badge.svg)](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/actions/workflows/dotnet.yml) [![NuGet Status](http://img.shields.io/nuget/v/Blazor.BFF.AzureAD.Template.svg?style=flat-square)](https://www.nuget.org/packages/Blazor.BFF.AzureAD.Template/) [Change log](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/blob/main/Changelog.md)

This template can be used to create a Blazor WASM application hosted in an ASP.NET Core Web app using Azure AD and Microsoft.Identity.Web to authenticate using the BFF security architecture. (server authentication) This removes the tokens from the browser and uses cookies with each HTTP request, response. The template also adds the required security headers as best it can for a Blazor application.
This template can be used to create a Blazor WASM application hosted in an ASP.NET Core Web app using Microsoft Entra ID and Microsoft.Identity.Web to authenticate using the BFF security architecture. (server authentication) This removes the tokens from the browser and uses cookies with each HTTP request, response. The template also adds the required security headers as best it can for a Blazor application.

![Blazor BFF Azure AD](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/blob/main/images/blazorBFFAzureAD.png)
![Blazor BFF Microsoft Entra ID](https://github.com/damienbod/Blazor.BFF.AzureAD.Template/blob/main/images/blazorBFFAzureAD.png)

## Features

- WASM hosted in ASP.NET Core 7
- BFF with Azure AD using Microsoft.Identity.Web
- WASM hosted in ASP.NET Core 8
- BFF with Microsoft Entra ID using Microsoft.Identity.Web
- OAuth2 and OpenID Connect OIDC
- No tokens in the browser
- Azure AD Continuous Access Evaluation CAE support
- Microsoft Entra ID Continuous Access Evaluation CAE support

## Other templates

Expand All @@ -38,7 +38,7 @@ Use the `-n` or `--name` parameter to change the name of the output created. Thi

## Setup after installation

Add the Azure AD App registration settings
Add the Microsoft Entra ID App registration settings

```
{
Expand Down Expand Up @@ -191,7 +191,7 @@ nuget pack content/Blazor.BFF.AzureAD.Template.nuspec
Locally built nupkg:

```
dotnet new install Blazor.BFF.AzureAD.Template.2.2.0.nupkg
dotnet new install Blazor.BFF.AzureAD.Template.3.0.0.nupkg
```

Local folder:
Expand All @@ -206,7 +206,7 @@ Where `<PATH>` is the path to the folder containing .template.config.

https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app

## Credits, Used NuGet packages + ASP.NET Core 7.0 standard packages
## Credits, Used NuGet packages + ASP.NET Core 8.0 standard packages

- NetEscapades.AspNetCore.SecurityHeaders

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.2.0</version>
<version>3.0.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 XSS block header</releaseNotes>
<releaseNotes>Updated .NET 8 packages, Microsoft Entra ID</releaseNotes>
<repository type="git" url="https://github.com/damienbod/Blazor.BFF.AzureAD.Template" />
<packageTypes>
<packageType name="Template" />
Expand Down
3 changes: 2 additions & 1 deletion content/BlazorBffAzureAD/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"classifications": [
"AspNetCore",
"WASM",
"MicrosoftEntraID"
"AzureAD",
"OAuth2",
"Web",
Expand All @@ -11,7 +12,7 @@
"Solution",
"Blazor"
],
"name": "ASP.NET Core Blazor BFF hosted WASM Azure AD",
"name": "ASP.NET Core Blazor BFF hosted WASM Microsoft Entra ID",
"identity": "Blazor.BFF.AzureAD.Template",
"shortName": "blazorbffaad",
"tags": {
Expand Down
10 changes: 5 additions & 5 deletions content/BlazorBffAzureAD/Client/BlazorBffAzureAD.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

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

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion content/BlazorBffAzureAD/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@page "/"

<h1>Azure AD using cookies</h1>
<h1>Microsoft Entra ID using cookies</h1>
2 changes: 1 addition & 1 deletion content/BlazorBffAzureAD/Client/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">Blazor AAD BFF Cookies</a>
<a class="navbar-brand" href="">Blazor ME ID BFF Cookies</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down
12 changes: 6 additions & 6 deletions content/BlazorBffAzureAD/Server/BlazorBffAzureAD.Server.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -12,11 +12,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.13" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.13" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.15.3" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.15.3" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.15.3" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.0" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.0" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.16.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.16.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.16.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.21.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion content/BlazorBffAzureAD/Server/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Blazor AAD Cookie</title>
<title>Blazor ME ID Cookie</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/app.css" rel="stylesheet" />
Expand Down
66 changes: 35 additions & 31 deletions content/BlazorBffAzureAD/Server/Services/MsGraphService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,49 @@
using Microsoft.Identity.Web;
using Microsoft.IdentityModel.Tokens;

namespace BlazorBffAzureAD.Server.Services
namespace BlazorBffAzureAD.Server.Services;

public class MsGraphService
{
public class MsGraphService
private readonly GraphServiceClient _graphServiceClient;

public MsGraphService(GraphServiceClient graphServiceClient)
{
private readonly GraphServiceClient _graphServiceClient;
_graphServiceClient = graphServiceClient;
}

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

public async Task<User?> GetGraphApiUser()
public async Task<string> GetGraphApiProfilePhoto()
{
try
{
return await _graphServiceClient.Me
.GetAsync(b => b.Options.WithScopes("User.ReadBasic.All", "user.read"));
}
var photo = string.Empty;
byte[] photoByte;
var streamPhoto = new MemoryStream();

public async Task<string> GetGraphApiProfilePhoto()
{
try
{
var photo = string.Empty;
// Get user photo
using (var photoStream = await _graphServiceClient
.Me
.Photo
.Content
.GetAsync(b => b.Options.WithScopes("User.ReadBasic.All", "user.read")))
{
byte[] photoByte = ((MemoryStream)photoStream!).ToArray();
photo = Base64UrlEncoder.Encode(photoByte);
}

return photo;
}
catch
// Get user photo
using (var photoStream = await _graphServiceClient
.Me
.Photo
.Content
.GetAsync(b => b.Options.WithScopes("User.ReadBasic.All", "user.read")))
{
return string.Empty;
photoStream!.CopyTo(streamPhoto);
photoByte = streamPhoto!.ToArray();
}

photo = Base64UrlEncoder.Encode(photoByte);

return photo;
}
catch
{
return string.Empty;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Loading