Welcome to SwaAuth! This library is designed to simplify the integration of Azure Static Web App authentication into your Blazor WASM and .NET Azure Function applications.
Before you begin, make sure you have the following:
- A Blazor WASM Application
- A .NET Azure Function with HTTP triggers (if using an API in your Azure Static Web App)
The NuGet package is built using .NET Standard 2.1
There are 2 packages to be installed. One for the Blazor WASM Client, the other for the .NET Azure Function API.
Either search for StacyClouds.SwaAuth.Client
in the NuGet package manager of your IDE or run the following command in the folder of your Blazor WASM project
dotnet add package StacyClouds.SwaAuth.Client
Either search for StacyClouds.SwaAuth.Api
in the NuGet package manager of your IDE or run the following command in the folder of your .NET Azure Function project
dotnet add package StacyClouds.SwaAuth.Api
Once SwaAuth is installed and configured, you can start using it in your application. Here are some examples:
To use SwaAuth in your Blazor WASM application add the following line to your Program.cs
file:
builder.Services.AddStaticWebAppsAuthentication();
The code above adds the Microsoft.AspNetCore.Components.Authorization NuGet package as well as adding an AuthenticationStateProvider
to the service collection.
Once this has been added, use auth in Blazor in the standard way. For more information see the Microsoft documentation.
In order to extract the Authentication information from the call to the Azure Function, you can use the StaticWebAppApiAuthentication
class. This class contains a method called GetClientPrincipal
which will extract the client principal from the request headers.
var clientPrincipal = StaticWebAppApiAuthentication.GetClientPrincipal(request.Headers);
The library does not contain code to perform direct authentication, as this is handled by the Azure Static Web App itself. The GetClientPrincipal
method will return a ClientPrincipal
object which contains the user's identity and roles contained in the headers the Azure Static Web Application injects into the request when directing it to the Api
Contributions are welcome! If you would like to contribute to SwaAuth, please follow the guidelines outlined in the CONTRIBUTING.md file.
This project is licensed under the MIT License. See the LICENSE file for more details.