Giter Site home page Giter Site logo

damienbod / blazor.bff.azuread.template Goto Github PK

View Code? Open in Web Editor NEW
74.0 4.0 12.0 4.44 MB

Blazor.BFF.AzureAD.Template, Blazor WASM hosted in ASP.NET Core using Microsoft Entra ID BFF (server authentication)

Home Page: https://www.nuget.org/packages/Blazor.BFF.AzureAD.Template/

License: MIT License

HTML 13.12% C# 36.34% CSS 50.16% JavaScript 0.38%
blazor aad azuread template oidc oauth2 csp microsoftidentity dotnet microsoftentraid

blazor.bff.azuread.template's Introduction

Blazor.BFF.AzureAD.Template (Microsoft Entra ID)

.NET NuGet Status Change log

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 Microsoft Entra ID

Features

  • 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
  • Microsoft Entra ID Continuous Access Evaluation CAE support

Other templates

Blazor BFF Azure B2C

Blazor BFF OpenID Connect

Using the template

install

dotnet new install Blazor.BFF.AzureAD.Template

run

dotnet new blazorbffaad -n YourCompany.Bff

Use the -n or --name parameter to change the name of the output created. This string is also used to substitute the namespace name in the .cs file for the project.

Setup after installation

Add the Microsoft Entra ID App registration settings

{
  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]",
    "TenantId": "[Enter 'common', or 'organizations' or the Tenant Id (Obtained from the Azure portal. Select 'Endpoints' from the 'App registrations' blade and use the GUID in any of the URLs), e.g. da41245a5-11b3-996c-00a8-4d99re19f292]",
    "ClientId": "[Enter the Client Id (Application ID obtained from the Azure portal), e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",
    "ClientSecret": "[Copy the client secret added to the app from the Azure portal]",
    "ClientCertificates": [
    ],
    // the following is required to handle Continuous Access Evaluation challenges
    "ClientCapabilities": [ "cp1" ],
    "CallbackPath": "/signin-oidc"
  },

Add the scopes for the downstream API if required

  "DownstreamApi": {
    "Scopes": "User.ReadBasic.All user.read"
  },

Use Continuous Access Evaluation CAE with a downstream API (access_token)

Azure app registration manifest

"optionalClaims": {
	"idToken": [],
	"accessToken": [
		{
			"name": "xms_cc",
			"source": null,
			"essential": false,
			"additionalProperties": []
		}
	],
	"saml2Token": []
},

Any API call for the Blazor WASM could be implemented like this:

[HttpGet]
public async Task<IActionResult> Get()
{
  try
  {
	// Do logic which calls an API and throws claims challenge 
	// WebApiMsalUiRequiredException. The WWW-Authenticate header is set
	// using the OpenID Connect Events and Signals spec.
  }
  catch (WebApiMsalUiRequiredException hex)
  {
	var claimChallenge = WwwAuthenticateParameters
		.GetClaimChallengeFromResponseHeaders(hex.Headers);
		
	return Unauthorized(claimChallenge);
  }
}

The downstream API call could be implemented something like this:

public async Task<T> CallApiAsync(string url)
{
	var client = _clientFactory.CreateClient();

	// ... add bearer token
	
	var response = await client.GetAsync(url);
	if (response.IsSuccessStatusCode)
	{
		var stream = await response.Content.ReadAsStreamAsync();
		var payload = await JsonSerializer.DeserializeAsync<T>(stream);

		return payload;
	}

	// You can check the WWW-Authenticate header first, if it is a CAE challenge
	
	throw new WebApiMsalUiRequiredException($"Error: {response.StatusCode}.", response);
}

Use Continuous Access Evaluation CAE in a standalone app (id_token)

Azure app registration manifest

"optionalClaims": {
	"idToken": [
		{
			"name": "xms_cc",
			"source": null,
			"essential": false,
			"additionalProperties": []
		}
	],
	"accessToken": [],
	"saml2Token": []
},

If using a CAE Authcontext in a standalone project, you only need to challenge against the claims in the application.

private readonly CaeClaimsChallengeService _caeClaimsChallengeService;

public AdminApiCallsController(CaeClaimsChallengeService caeClaimsChallengeService)
{
  _caeClaimsChallengeService = caeClaimsChallengeService;
}

[HttpGet]
public IActionResult Get()
{
  // if CAE claim missing in id token, the required claims challenge is returned
  var claimsChallenge = _caeClaimsChallengeService
	.CheckForRequiredAuthContextIdToken(AuthContextId.C1, HttpContext);

  if (claimsChallenge != null)
  {
	return Unauthorized(claimsChallenge);
  }

uninstall

dotnet new uninstall Blazor.BFF.AzureAD.Template

Development

build

https://docs.microsoft.com/en-us/dotnet/core/tutorials/create-custom-template

nuget pack content/Blazor.BFF.AzureAD.Template.nuspec

install developement

Locally built nupkg:

dotnet new install Blazor.BFF.AzureAD.Template.3.1.1.nupkg

Local folder:

dotnet new install <PATH>

Where <PATH> is the path to the folder containing .template.config.

Azure App registrations documentation

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

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

  • NetEscapades.AspNetCore.SecurityHeaders

Links

https://github.com/AzureAD/microsoft-identity-web

https://damienbod.com/2022/04/20/implement-azure-ad-continuous-access-evaluation-in-an-asp-net-core-razor-page-app-using-a-web-api/

https://github.com/damienbod/AspNetCoreAzureADCAE

blazor.bff.azuread.template's People

Contributors

damienbod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

blazor.bff.azuread.template's Issues

Signout redirect to home page

Hi Damien, thanks for this.

Could you give any hints on how to redirect user back to the home page after sign out?
I have tried all sort of tricks found on the internet, but I can't get past the "You signed out of your account
It's a good idea to close all browser windows." page at https://login.microsoftonline.com/common/oauth2/v2.0/logoutsession.

I'm using personal Microsoft accounts in my Azure Ad App registration, I read somewhere that that might prevent the redirect..?
Any advice on this is appreciated, thanks!

.NET 6 minimal hosting

This is a great template! Thank you for creating this.
It would be great to have the server project use the .NET 6 minimal hosting approach. But this is an easy change after creating the project from the template.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.