A simple basic authentication middleware. Allows setup authentication using configuration or dependency injection
and suppress WWW-Authenticate header globally or for AJAX request.
Install using the ZNetCS.AspNetCore.Authentication.Basic NuGet package
PM> Install-Package ZNetCS.AspNetCore.Authentication.Basic
Cleanup events initialization and nullable checkup. Events are now only initialized in handler not in options. Unless configured during initialization (no change in code is required, it is just code cleanup). Logger improvements.
Added direct references to latest framework and removed no longer supported frameworks. Added possibility to suppress WWWAuthenticate header globally not only on Ajax request.
From now assembly is signed.
The OnValidatePrincipal will not return AuthenticationResult any more. To simplify process can simply return Task.CompletedTask.
Also to make success authentication Principal have to be assigned to ValidatePrincipalContext context.
When you install the package, it should be added to your .csproj. Alternatively, you can add it directly by adding:
<ItemGroup>
<PackageReferenceInclude="ZNetCS.AspNetCore.Authentication.Basic"Version="10.0.0" />
</ItemGroup>usingZNetCS.AspNetCore.Authentication.Basic;usingZNetCS.AspNetCore.Authentication.Basic.Events;...
In order to use the basic authentication middleware, you must configure the services in the Program.cs file.
// Add services to the container.builder.Services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme).AddBasicAuthentication(
options =>{options.Realm="My Application";options.Events=newBasicAuthenticationEvents{OnValidatePrincipal= context =>{if((context.UserName=="userName")&&(context.Password=="password")){varclaims=newList<Claim>{newClaim(ClaimTypes.Name,context.UserName,context.Options.ClaimsIssuer)};varprincipal=newClaimsPrincipal(newClaimsIdentity(claims,context.Scheme.Name));context.Principal=principal;}else{// optional with following default.// context.AuthenticationFailMessage = "Authentication failed."; }returnTask.CompletedTask;}};});or using dependency injection
publicclassAuthenticationEvents:BasicAuthenticationEvents{
#region Public Methods
/// <inheritdoc/>publicoverrideTaskValidatePrincipalAsync(ValidatePrincipalContextcontext){if((context.UserName=="userName")&&(context.Password=="password")){varclaims=newList<Claim>{newClaim(ClaimTypes.Name,context.UserName,context.Options.ClaimsIssuer)};varprincipal=newClaimsPrincipal(newClaimsIdentity(claims,context.Scheme.Name));context.Principal=principal;}returnTask.CompletedTask;}
#endregion
}and then registration
builder.Services.AddScoped<AuthenticationEvents>();builder.Services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme).AddBasicAuthentication(
options =>{options.Realm="My Application";options.EventsType=typeof(AuthenticationEvents);});then
// configure default authentication initializationapp.UseAuthentication();// other middleware e.g. MVC etcIn order to use the basic authentication middleware, you must configure the services in the Configure and ConfigureServices call of Startup. Because basic
authentication is manual process handled on each request, there is need to validate credentials manually (see below).
publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv,ILoggerFactoryloggerFactory){// default authentication initializationapp.UseAuthentication();// other middleware e.g. MVC etc}publicvoidConfigureServices(IServiceCollectionservices){services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme).AddBasicAuthentication(
options =>{options.Realm="My Application";options.Events=newBasicAuthenticationEvents{OnValidatePrincipal= context =>{if((context.UserName=="userName")&&(context.Password=="password")){varclaims=newList<Claim>{newClaim(ClaimTypes.Name,context.UserName,context.Options.ClaimsIssuer)};varprincipal=newClaimsPrincipal(newClaimsIdentity(claims,context.Scheme.Name));context.Principal=principal;}else{// optional with following default.// context.AuthenticationFailMessage = "Authentication failed."; }returnTask.CompletedTask;}};});}or using dependency injection:
publicclassAuthenticationEvents:BasicAuthenticationEvents{
#region Public Methods
/// <inheritdoc/>publicoverrideTaskValidatePrincipalAsync(ValidatePrincipalContextcontext){if((context.UserName=="userName")&&(context.Password=="password")){varclaims=newList<Claim>{newClaim(ClaimTypes.Name,context.UserName,context.Options.ClaimsIssuer)};varprincipal=newClaimsPrincipal(newClaimsIdentity(claims,context.Scheme.Name));context.Principal=principal;}returnTask.CompletedTask;}
#endregion
}and then registration
publicvoidConfigureServices(IServiceCollectionservices){services.AddScoped<AuthenticationEvents>();services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme).AddBasicAuthentication(
options =>{options.Realm="My Application";options.EventsType=typeof(AuthenticationEvents);});}As from version 3.0.1 You can suppress the response WWW-Authenticate header (avoiding the browser to show a popup) for ajax requests by using a switch.
publicvoidConfigureServices(IServiceCollectionservices){services.AddScoped<AuthenticationEvents>();services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme).AddBasicAuthentication(
options =>{options.Realm="My Application";options.AjaxRequestOptions.SuppressWwwAuthenticateHeader=true;});}