BeamCode.Extensions.AspNetCore
1.0.2
dotnet add package BeamCode.Extensions.AspNetCore --version 1.0.2
NuGet\Install-Package BeamCode.Extensions.AspNetCore -Version 1.0.2
<PackageReference Include="BeamCode.Extensions.AspNetCore" Version="1.0.2" />
paket add BeamCode.Extensions.AspNetCore --version 1.0.2
#r "nuget: BeamCode.Extensions.AspNetCore, 1.0.2"
// Install BeamCode.Extensions.AspNetCore as a Cake Addin #addin nuget:?package=BeamCode.Extensions.AspNetCore&version=1.0.2 // Install BeamCode.Extensions.AspNetCore as a Cake Tool #tool nuget:?package=BeamCode.Extensions.AspNetCore&version=1.0.2
Beam.Extensions.AspNetCore
A Set of ASP.NET Core 7 extensions to register DbContext, identity, JWT, localization, compression...
These are some useful methods that I use on my regular projects.
Add & Register & Configure Services
Adding Localization
// Adding localization ( you can also provide the directory for the .resex files by
// default is "Resources".
builder.Services.AddLocalizationResource()
// Configure localization ( adding languages to the app).
.ConfigureLocalization(cultures: new List<string>
{
"en",
"es",
"de",
"ar"
}, mainCulture: "en" /* Main language will be english */);
Razor Pages Or MVC
While you use razor pages or mvc you also need to add the following to be able to use localization for the views.
builder.Services.AddControllersWithViews()
// We need this line to add localization for the views.
.AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix);
Registering DbContext
The Current registration uses scoped lifetime cycle to register the DbContext. Store the connectionString in appsettings.json and provide the name in connectionString name as showed below as "Development", you also need to provide the time out time (in seconds) as showed as 30.
builder.Services.RegisterDbContext<SampleContext>(
configuration: builder.Configuration,
connectionStringName: "Development",
timeOut: 30);
Add JWT Bear Token As Identity Service
You can add JWT bearer token identity by adding following.
builder.Services.AddJwtService();
Default options are :
ValidateIssuer = false,
ValidateAudience = false,
RequireExpirationTime = true,
ClockSkew = TimeSpan.Zero
You can also configure the options:
builder.Services.AddJwtService(options =>
{
options.TokenValidationParameters = new TokenValidationParameters(
{
ValidateIssuer = false,
ValidateAudience = true,
...
};
});
Add Identity
You can add identity service using extended identity users and roles.
builder.Services.AddIdentityService<SampleContext, ApplicationUser, Role, Guid>();
Default identity options are :
Password.RequireDigit = true;
Password.RequiredLength = 5;
Password.RequireUppercase = true;
Password.RequireLowercase = true;
Password.RequireNonAlphanumeric = false;
SignIn.RequireConfirmedEmail = true;
User.RequireUniqueEmail = true;
Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
Lockout.MaxFailedAccessAttempts = 5;
You can also configure the identity options:
builder.Services.AddIdentityService<SampleContext, ApplicationUser, Role, Guid>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 10;
...
});
Set TokeLifeSpan
Set token life span to 30 minutes.
builder.Services.ConfigureDataProtection(TimeSpan.FromMinutes(30));
Add Policy Authorizations
You can add the policy based authorizations easily using the method bellow
builder.Services.AddAuthorizations(new Dictionary<string, string[]>
{
{
"Admin" /* Policy name */,
new []
{
"Administrator",
"SuperAdmin"
} /* Required roles to meet the policy. */
},
{
"Employee" /* Policy name */,
new []
{
"Employee"
} /* Required roles to meet the policy. */
}
});
Add Response Compression Using (Brotli & GZip)
- Registering Response Compression
builder.Services.RegisterResponseCompression();
The MimeTypes used to compression can be found Here
You can also configure the registration of response compression
builder.Services.RegisterResponseCompression(options =>
{
options.EnableForHttps = true;
...
});
- Configure Response Compression Providers
builder.Services.ConfigureResponseCompression();
Default : <br> Brotli Compression = Fastest <br> GZip Compression = SmallestSize
You can also configure the response compression providers
builder.Services.ConfigureResponseCompression<BrotliCompressionProviderOptions,GzipCompressionProviderOptions>(brotliCompressionLevel: CompressionLevel.Fastest,
gzipCompressionLevel: CompressionLevel.SmallestSize);
Using (Middlewares)
Use Response Compression
Use the bellow middleware to to be able to use response compression.
app.UseResponseCompression();
Use Localization
Use the bellow middleware to to be able to use localization.
app.UseLocalization();
Use Authentication (Identity)
Note: Authentication middleware must come before authorization.
app.UseAuthentication();
app.UseAuthorization();
Fluent?
All the methods return IServiceCollection which means you can chain the methods (better to only chain related methods), you can find the sample to see the whole implementation.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 7.0.0)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 7.0.0)
- Microsoft.EntityFrameworkCore (>= 7.0.0)
- Microsoft.EntityFrameworkCore.SqlServer (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.