AnthonyChu.AzureStaticWebApps.Blazor.Authentication 0.0.2-preview

This is a prerelease version of AnthonyChu.AzureStaticWebApps.Blazor.Authentication.
dotnet add package AnthonyChu.AzureStaticWebApps.Blazor.Authentication --version 0.0.2-preview
NuGet\Install-Package AnthonyChu.AzureStaticWebApps.Blazor.Authentication -Version 0.0.2-preview
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AnthonyChu.AzureStaticWebApps.Blazor.Authentication" Version="0.0.2-preview" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AnthonyChu.AzureStaticWebApps.Blazor.Authentication --version 0.0.2-preview
#r "nuget: AnthonyChu.AzureStaticWebApps.Blazor.Authentication, 0.0.2-preview"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AnthonyChu.AzureStaticWebApps.Blazor.Authentication as a Cake Addin
#addin nuget:?package=AnthonyChu.AzureStaticWebApps.Blazor.Authentication&version=0.0.2-preview&prerelease

// Install AnthonyChu.AzureStaticWebApps.Blazor.Authentication as a Cake Tool
#tool nuget:?package=AnthonyChu.AzureStaticWebApps.Blazor.Authentication&version=0.0.2-preview&prerelease

Blazor Authentication Extension for Azure Static Web Apps

Use Azure Static Web Apps authentication in Blazor WebAssembly apps.

Usage

Install NuGet package

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().

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>("app");

            builder.Services
                .AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) })
                .AddStaticWebAppsAuthentication();
            await builder.Build().RunAsync();
        }
    }
}

Update App.razor

Add <CascadingAuthenticationState> and <AuthorizeRouteView> to App.razor. For more information, check out the Blazor security docs.

<CascadingAuthenticationState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        </Found>
        <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
        </NotFound>
    </Router>
</CascadingAuthenticationState>

Log user into Azure Static Web Apps using social

Redirect the user to /.auth/login/<social-provider> to log them in. More info at the Static Web Apps authentication docs.

Access user identity

Use context.User to get information about the user.

<AuthorizeView>
    <Authorized>
        Hello, @context.User.Identity.Name!
        <a href="/.auth/logout?post_logout_redirect_uri=/">Log out</a>
    </Authorized>
    <NotAuthorized>
        <a href="/login-providers">Log in</a>
    </NotAuthorized>
</AuthorizeView>

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:

{
  "StaticWebAppsAuthentication": {
    "AuthenticationDataUrl": "/sample-data/me.json"
  }
}

See sample-data/me.json for an example of the structure. For more information, check out the Static Web Apps docs.

Sample app

Check out the sample app at sample/app.


This is not an officially supported Microsoft project.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on AnthonyChu.AzureStaticWebApps.Blazor.Authentication:

Repository Stars
microsoft/AzUrlShortener
An simple and easy Url Shortener
Version Downloads Last updated
0.0.2-preview 13,473 7/28/2020
0.0.1-preview 319 7/27/2020