RealtimeDatabase 1.0.2
The project was renamed to SapphireDb
See the version list below for details.
dotnet add package RealtimeDatabase --version 1.0.2
NuGet\Install-Package RealtimeDatabase -Version 1.0.2
<PackageReference Include="RealtimeDatabase" Version="1.0.2" />
paket add RealtimeDatabase --version 1.0.2
#r "nuget: RealtimeDatabase, 1.0.2"
// Install RealtimeDatabase as a Cake Addin #addin nuget:?package=RealtimeDatabase&version=1.0.2 // Install RealtimeDatabase as a Cake Tool #tool nuget:?package=RealtimeDatabase&version=1.0.2
Realtime Database - Server Configuration
Installation
Install Package
To use the realtime database on server side your first need to install the nuget package.
In an Asp.Net Core project execute
PM > Install-Package RealtimeDatabase
https://www.nuget.org/packages/RealtimeDatabase/
Configure DbContext
Create a new context or use an existing and derive from RealtimeDbContext
// Change DbContext to RealtimeDbContext
public class MyDbContext : RealtimeDbContext
{
//Add RealtimeDatabaseNotifier for DI
public RealtimeContext(DbContextOptions<RealtimeContext> options, RealtimeDatabaseNotifier notifier) : base(options, notifier)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Test> Tests { get; set; }
...
}
Register the realtime database service
In the service configuration (normally in Startup.cs) add your DbContext and RealtimeDatabase
public void ConfigureServices(IServiceCollection services)
{
...
// Register services
services.AddRealtimeDatabase<MyDbContext>();
services.AddDbContext<MyDbContext>(cfg => ...));
...
}
Configure Request Pipeline
Add RealtimeDatabase in your pipeline configuration
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
//Add Middleware
app.UseRealtimeDatabase();
}
When using Authentication make sure to call it before RealtimeDatabase
Configuration
Make Entity Properties Updatable
To make properties of an Entity updatable using the update method of the realtime collection
you have to add the UpdatableAttribute
to the class or properties of it. All other properties
cannot be changed using the realtime methods at client side.
Make all properties of the class updatable:
[Updatable]
public class User : Base
{
[Required]
[MinLength(3)]
public string Username { get; set; }
[Required]
[MinLength(3)]
public string FirstName { get; set; }
[Required]
[MinLength(3)]
public string LastName { get; set; }
}
Only make Username updatable:
public class User : Base
{
[Updatable]
[Required]
[MinLength(3)]
public string Username { get; set; }
[Required]
[MinLength(3)]
public string FirstName { get; set; }
[Required]
[MinLength(3)]
public string LastName { get; set; }
}
Authentication/Authorization
You can protect specific actions on entity classes by using the attributes
QueryAuthAttribute
, CreateAuthAttribute
, UpdateAuthAttribute
and RemoveAuthAttribute
.
If you just use the plain attributes without any configuration they will just enable authentication for the specific action and model.
For example:
[QueryAuth] // Will require an authenticated request to allow query users
[RemoveAuth] // Will require an authenticated request to allow remove users
// All other operations are allowed without authentication
public class User : Base
{
[Required]
[MinLength(3)]
public string Username { get; set; }
[Required]
[MinLength(3)]
public string FirstName { get; set; }
[Required]
[MinLength(3)]
public string LastName { get; set; }
}
You can also define roles that are authorized to perform a specific action:
[QueryAuth] // Will require an authenticated request to allow query
[RemoveAuth("admin")] // Will require an authenticated request and role
// 'admin' to allow remove
public class User : Base
{
[Required]
[MinLength(3)]
public string Username { get; set; }
[Required]
[MinLength(3)]
public string FirstName { get; set; }
[Required]
[MinLength(3)]
public string LastName { get; set; }
}
The QueryAuthAttribute
and UpdateAuthAttribute
can also be used for properties.
You can use it to control query or update of a specific property.
If a property is not queryable beacause the user is not authorized to it is just omitted
and does not get transmitted to the client. The same behavior is used when
an update of a property is not allowed for a user: The property just is omitted and not changed.
[QueryAuth]
public class User : Base
{
[Required]
[MinLength(3)]
public string Username { get; set; }
[Required]
[MinLength(3)]
[QueryAuth("admin")] // Property FirstName can only get queried by
// users with role `admin`
public string FirstName { get; set; }
[Required]
[MinLength(3)]
[Updatable]
[UpdateAuth("admin")] // LastName can only get updated by users with role
// `admin`
public string LastName { get; set; }
}
Realtime Auth
Realtime database comes with a JWT Provider. The client can call the methods for login etc. over websocket.
You only have to add this to your appsettings.json:
{
"JwtOptions": {
"Issuer": "RealtimeDatabase",
"SecretKey": "secret",
"ValidFor": 60
}
}
You have to enable the provider using:
services.AddRealtimeAuth<RealtimeAuthContext<AppUser>, AppUser>(
new JwtOptions(Configuration.GetSection(nameof(JwtOptions))),
cfg => cfg.UseFileContext(databasename: "auth"));
Also add this to your request pipeline:
app.UseRealtimeAuth();
Now the client can login using realtime database authentication.
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. |
.NET Core | netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.1
- Microsoft.AspNetCore.All (>= 2.1.5)
- Microsoft.EntityFrameworkCore (>= 2.1.4)
- Newtonsoft.Json (>= 11.0.2)
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 | |
---|---|---|---|
3.4.0 | 817 | 11/24/2019 | |
3.3.1 | 687 | 11/6/2019 | |
3.3.0 | 669 | 11/1/2019 | |
3.2.0 | 676 | 10/13/2019 | |
3.1.0 | 733 | 9/8/2019 | |
3.0.4 | 695 | 8/14/2019 | |
3.0.3 | 675 | 8/13/2019 | |
3.0.2 | 704 | 8/9/2019 | |
3.0.1 | 706 | 8/9/2019 | |
3.0.0 | 691 | 8/7/2019 | |
2.0.1 | 748 | 7/28/2019 | |
2.0.0 | 702 | 7/14/2019 | |
1.0.6 | 771 | 3/22/2019 | |
1.0.5 | 713 | 3/21/2019 | |
1.0.4 | 757 | 3/11/2019 | |
1.0.3 | 834 | 1/29/2019 | |
1.0.2 | 1,668 | 12/8/2018 | |
1.0.1 | 1,696 | 10/26/2018 | |
1.0.0 | 1,671 | 10/3/2018 |