An open API service indexing awesome lists of open source software.

https://github.com/bytexdigital/bytexdigital.blazor.server.authentication

Authentication library for serversided Blazor
https://github.com/bytexdigital/bytexdigital.blazor.server.authentication

Last synced: about 1 year ago
JSON representation

Authentication library for serversided Blazor

Awesome Lists containing this project

README

          

# 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.

> :warning: **This library is meant only for server-sided Blazor.**

## Download
[:arrow_forward: **BytexDigital.Blazor.Server.Authentication** Nuget package](https://www.nuget.org/packages/BytexDigital.Blazor.Server.Authentication/)
![nuget BytexDigital.Blazor.Server.Authentication](https://img.shields.io/nuget/vpre/BytexDigital.Blazor.Server.Authentication.svg?style=flat-square)

[:arrow_forward: **BytexDigital Blazor.Server.Authentication.Identity** Nuget package](https://www.nuget.org/packages/BytexDigital.Blazor.Server.Authentication.Identity/)
![nuget BytexDigital.Blazor.Server.Authentication.Identity](https://img.shields.io/nuget/vpre/BytexDigital.Blazor.Server.Authentication.Identity.svg?style=flat-square)

## 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
```csharp
services.AddHttpContextAccessor();
```

##### With Identity
```csharp
services
.AddAuthenticationService()
.AddCookiePrincipalStorage()
.AddIdentityPrincipalProvider();
```

##### Without Identity
```csharp
services
.AddAuthenticationService()
.AddCookiePrincipalStorage()
.AddPrincipalProvider();
```

#### 3. Add the Javascript to your `_Host.cshtml`
```html

```

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

```cshtml










```

## 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`.