https://github.com/anthonychu/blazor-auth-static-web-apps
A Blazor WebAssembly AuthenticationStateProvider for Azure Static Web Apps
https://github.com/anthonychu/blazor-auth-static-web-apps
Last synced: about 1 year ago
JSON representation
A Blazor WebAssembly AuthenticationStateProvider for Azure Static Web Apps
- Host: GitHub
- URL: https://github.com/anthonychu/blazor-auth-static-web-apps
- Owner: anthonychu
- License: mit
- Created: 2020-07-25T23:45:25.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2021-06-23T06:25:32.000Z (almost 5 years ago)
- Last Synced: 2025-03-26T20:55:41.093Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 246 KB
- Stars: 36
- Watchers: 5
- Forks: 14
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Blazor Authentication Extension for Azure Static Web Apps
Use Azure Static Web Apps authentication in Blazor WebAssembly apps.
## Usage
### Install NuGet package
```bash
dotnet add package AnthonyChu.AzureStaticWebApps.Blazor.Authentication --version 0.0.2-preview
```
### Update Program.cs
Add `using AzureStaticWebApps.Blazor.Authentication` and register services with `AddStaticWebAppsAuthentication()`.
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using AzureStaticWebApps.Blazor.Authentication;
namespace BlazorLogin
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add("app");
builder.Services
.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) })
.AddStaticWebAppsAuthentication();
await builder.Build().RunAsync();
}
}
}
```
### Update App.razor
Add `` and `` to App.razor. For more information, check out the [Blazor security docs](https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-3.1#customize-unauthorized-content-with-the-router-component).
```html
Sorry, there's nothing at this address.
```
### Log user into Azure Static Web Apps using social
Redirect the user to `/.auth/login/` to log them in. More info at the [Static Web Apps authentication docs](https://docs.microsoft.com/en-us/azure/static-web-apps/authentication-authorization).
### Access user identity
Use `context.User` to get information about the user.
```html
Hello, @context.User.Identity.Name!
Log out
Log in
```
## Configuration
By default, the auth provider will call `/.auth/me` to determine if a user is logged in and get information about the logged in user. Configure `StaticWebAppsAuthentication:AuthenticationDataUrl` in appsettings.json (or an envionrment specific version) to use a different endpoint.
For instance, in local development you can use a static file:
```json
{
"StaticWebAppsAuthentication": {
"AuthenticationDataUrl": "/sample-data/me.json"
}
}
```
See [sample-data/me.json](sample/app/wwwroot/sample-data/me.json) for an example of the structure. For more information, check out the [Static Web Apps docs](https://docs.microsoft.com/en-us/azure/static-web-apps/user-information).
## Sample app
Check out the sample app at [sample/app](sample/app).
---
This is not an officially supported Microsoft project.