DataHead.ApiToolkit
1.0.0
dotnet add package DataHead.ApiToolkit --version 1.0.0
NuGet\Install-Package DataHead.ApiToolkit -Version 1.0.0
<PackageReference Include="DataHead.ApiToolkit" Version="1.0.0" />
<PackageVersion Include="DataHead.ApiToolkit" Version="1.0.0" />
<PackageReference Include="DataHead.ApiToolkit" />
paket add DataHead.ApiToolkit --version 1.0.0
#r "nuget: DataHead.ApiToolkit, 1.0.0"
#:package DataHead.ApiToolkit@1.0.0
#addin nuget:?package=DataHead.ApiToolkit&version=1.0.0
#tool nuget:?package=DataHead.ApiToolkit&version=1.0.0
API Toolkit
This package contains a set of various tools that can be used during the development of a Web API.
Identity and Authentication
This package contains a permissions system build on top of .NET Identity. All that needs to be supplied is an Enum containing application permissions. An example enum is listed here:
public enum Permission
{
CreateUsers = 101,
ReadUsers = 102,
UpdateUsers = 103,
DeleteUsers = 104,
CreateRoles = 201,
ReadRoles = 202,
UpdateRoles = 203,
DeleteRoles = 204,
}
To set this up, make sure your database context extends IdentityDbContext and specifies the following types:
public class ApplicationContext : IdentityDbContext<IdentityUser, RoleWithPermissions<Permission>, string> {
}
The RoleWithPermissions model required a Permissions enum to be specified. You can use the default IdentityUser
class or provide one of your own.
After this, you can call AddIdentity on the IServiceCollection using the RoleWithPermissions role class.
A helper function is included to configure authentication. You can call AddJwtAuthentication on
the IServiceCollection and provide a secret, audience, and issuer in order to configure JWT authentication.
In order for the permissions policies to function, you must call two more functions on the service collection.
builder.Services.AddAuthorizationWithPerms<Permission>();
builder.Services.AddPermissionHandler<Permission, IdentityUser, RoleWithPermissions<Permission>>();
If you want to ensure a SuperAdmin role is always available, you can call the following function before app.Run() is
called.
WebApplicationHelpers.GenerateDefaultRoles<Permission, RoleWithPermissions<Permission>>(builder.Services);
Make sure that authentication and authorization is set before the run function is called.
app.UseAuthentication();
app.UseAuthorization();
Models, View Models and the Generic Repository
A BaseModel class is provided for database models that provides CreatedAt and UpdatedAt date fields. If a
model extends from this class, then these values are updated if saved through the GenericRepository class. The
Generic Repository provides a repository class for creating and modifying database models. It also exposes the
underlying query system if it needs to be used. You can add a repository to the IOC container using the code below:
builder.Services.AddScoped<IGenericRepository<MyModel>, GenericRepository<MyModel>>();
API Response and Pagination
An ApiResponse class is provided. This standardizes the format of an API response from the server.
A PaginatedData class also exists that formats data along with pagination information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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. |
-
net6.0
- MediatR (>= 10.0.1)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 6.0.9)
- Microsoft.AspNetCore.Authorization (>= 6.0.9)
- Microsoft.AspNetCore.Authorization.Policy (>= 2.2.0)
- Microsoft.EntityFrameworkCore (>= 6.0.9)
- Microsoft.Extensions.Identity.Stores (>= 6.0.9)
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 |
|---|---|---|
| 1.0.0 | 390 | 9/23/2022 |