Skip to content

BytexDigital/BytexDigital.Blazor.Server.Authentication

Repository files navigation

BytexDigital.Blazor.Server.Authentication

This library adds a simple way of being able to sign in, sign out and remember the signed in user (e.g. via cookies) directly from Blazor without the need of redirecting the user to Razor pages.

Existing Blazor components such as AuthorizeView will continue to work as expected.

⚠️ This library is meant only for server-sided Blazor.

Download

▶️ BytexDigital.Blazor.Server.Authentication Nuget package nuget BytexDigital.Blazor.Server.Authentication

▶️ BytexDigital Blazor.Server.Authentication.Identity Nuget package nuget BytexDigital.Blazor.Server.Authentication.Identity

How to use?

1. Install BytexDigital.Blazor.Server.Authentication or BytexDigital.Blazor.Server.Authentication.Identity

2. Register the necessary services

Make sure to register the services after other calls to add Authentication services.

Required in all cases
services.AddHttpContextAccessor();
With Identity
services
    .AddAuthenticationService()
    .AddCookiePrincipalStorage()
    .AddIdentityPrincipalProvider<ApplicationUser>();
Without Identity
services
    .AddAuthenticationService()
    .AddCookiePrincipalStorage()
    .AddPrincipalProvider<ImplementationOfIPrincipalProvider>();

3. Add the Javascript to your _Host.cshtml

<script src="/_content/BytexDigital.Blazor.Server.Authentication/bundle.js"></script>

Edit your App.razor to wrap your content in a CascadingAuthenticationProvider

<BytexDigital.Blazor.Server.Authentication.CascadingAuthenticationProvider>
	<Router AppAssembly="@typeof(Program).Assembly">
		<Found Context="routeData">
			<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
		</Found>
		<NotFound>
			<LayoutView Layout="@typeof(MainLayout)">
				<NotFound></NotFound>
			</LayoutView>
		</NotFound>
	</Router>
</BytexDigital.Blazor.Server.Authentication.CascadingAuthenticationProvider>

Sign in and sign out

Use IServerAuthenticationService.SignInAsAsync and IServerAuthenticationService.SignOutAsync to change the signed in user. Reloading the page is not necessary. You can use IServerAuthenticationService.GetSignedInIdOrDefault and IServerAuthenticationService.IsSignedIn to get information about the current authentication status.

Questions

What is the IPrincipalProvider?

The IPrincipalProvider is used to convert a user id to a ClaimsPrincipal object. The included IdentityPrincipalProvider will convert the user id to a principal using a UserStore<TUser>.

About

Authentication library for serversided Blazor

Resources

License

Stars

Watchers

Forks

Packages

No packages published