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
- Host: GitHub
- URL: https://github.com/bytexdigital/bytexdigital.blazor.server.authentication
- Owner: BytexDigital
- License: apache-2.0
- Created: 2020-06-16T10:43:01.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-16T08:36:47.000Z (over 5 years ago)
- Last Synced: 2025-03-31T01:11:09.319Z (about 1 year ago)
- Language: C#
- Size: 46.9 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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/)

[:arrow_forward: **BytexDigital Blazor.Server.Authentication.Identity** Nuget package](https://www.nuget.org/packages/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
```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`.