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.
// 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 */);
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);
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);
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,
...
};
});
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 token life span to 30 minutes.
builder.Services.ConfigureDataProtection(TimeSpan.FromMinutes(30));
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. */
}
});
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;
...
});
builder.Services.ConfigureResponseCompression();
Default :
Brotli Compression = Fastest
GZip Compression = SmallestSize
You can also configure the response compression providers
builder.Services.ConfigureResponseCompression<BrotliCompressionProviderOptions,GzipCompressionProviderOptions>(brotliCompressionLevel: CompressionLevel.Fastest,
gzipCompressionLevel: CompressionLevel.SmallestSize);
Use the bellow middleware to to be able to use response compression.
app.UseResponseCompression();
Use the bellow middleware to to be able to use localization.
app.UseLocalization();
Note: Authentication middleware must come before authorization.
app.UseAuthentication();
app.UseAuthorization();
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.