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
<PackageReference Include="ShadySoft.Blazor.SignInService" Version="1.2.0" />
<PackageVersion Include="ShadySoft.Blazor.SignInService" Version="1.2.0" />
<PackageReference Include="ShadySoft.Blazor.SignInService" />
paket add ShadySoft.Blazor.SignInService --version 1.2.0
#r "nuget: ShadySoft.Blazor.SignInService, 1.2.0"
#:package ShadySoft.Blazor.SignInService@1.2.0
#addin nuget:?package=ShadySoft.Blazor.SignInService&version=1.2.0
#tool nuget:?package=ShadySoft.Blazor.SignInService&version=1.2.0
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:
- Add this line to _Host.cshtml inside the <head> tag:
<script src="_content/ShadySoft.Blazor.SignInService/shadyAuthHelpers.js"></script>
- 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>
- 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.
- 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 | Versions 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. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Components (>= 3.1.0)
- Microsoft.AspNetCore.Components.Web (>= 3.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.2.0 - Added an ISignInService interface to simplify dependency injection.