ShadySoft.Blazor.SignInService 1.2.0

dotnet add package ShadySoft.Blazor.SignInService --version 1.2.0
                    
NuGet\Install-Package ShadySoft.Blazor.SignInService -Version 1.2.0
                    
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="ShadySoft.Blazor.SignInService" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ShadySoft.Blazor.SignInService" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="ShadySoft.Blazor.SignInService" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ShadySoft.Blazor.SignInService --version 1.2.0
                    
#r "nuget: ShadySoft.Blazor.SignInService, 1.2.0"
                    
#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.
#:package ShadySoft.Blazor.SignInService@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ShadySoft.Blazor.SignInService&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=ShadySoft.Blazor.SignInService&version=1.2.0
                    
Install as a Cake Tool

My main goal was to enable the use of vanilla Identity without using the razor page UI. What I ended up with was a service that essentially wraps the SignInManager so that sign-in can be persisted without page reloads. This package doesn't include any UI.

Installation:

  1. Add this line to _Host.cshtml inside the <head> tag:
<script src="_content/ShadySoft.Blazor.SignInService/shadyAuthHelpers.js"></script>
  1. Wrap all App.razor content with a CascadingAuthenticationState component and replace the RouteView component with an AuthorizedRoutView component. Example:
<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>
  1. Use the AddBlazorIdentity extension method to register services in the Startup.ConfigureServices method:
services.AddBlazorIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

Notes: Any options provided as an argument to the AddBlazorIdentity extension method will be passed along to AddIdentity. Also, this example assumes an IdentityDbContext is used as an entity framework user store. See any of the Identity templates for the required code to set that up.

  1. In the Startup.Configure method, add UseAuthentication and UseAuthorization to the middleware pipeline just before UseEndpoints. Also, add MapControllers to the endpoints. Example:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
});

That's it. ISignInService is the interface that you'll use. It has three public methods, SignInAsync, SignOutAsync, and RefreshSignInAsync. That's really all this package does, but the lack of those methods was the major barrier to me embracing Blazor server-side and this takes care of them.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
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

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 720 1/27/2020
1.1.0 704 1/21/2020
1.0.0 611 1/21/2020

1.2.0 - Added an ISignInService interface to simplify dependency injection.