VOOZH about

URL: https://www.nuget.org/packages/ZNetCS.AspNetCore.Authentication.Basic/

⇱ NuGet Gallery | ZNetCS.AspNetCore.Authentication.Basic 10.0.0




ZNetCS.AspNetCore.Authentication.Basic 10.0.0

dotnet add package ZNetCS.AspNetCore.Authentication.Basic --version 10.0.0
 
 
NuGet\Install-Package ZNetCS.AspNetCore.Authentication.Basic -Version 10.0.0
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="10.0.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZNetCS.AspNetCore.Authentication.Basic" Version="10.0.0" />
 
Directory.Packages.props
<PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZNetCS.AspNetCore.Authentication.Basic --version 10.0.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZNetCS.AspNetCore.Authentication.Basic, 10.0.0"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZNetCS.AspNetCore.Authentication.Basic@10.0.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZNetCS.AspNetCore.Authentication.Basic&version=10.0.0
 
Install as a Cake Addin
#tool nuget:?package=ZNetCS.AspNetCore.Authentication.Basic&version=10.0.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ZNetCS.AspNetCore.Authentication.Basic

👁 NuGet
👁 Build

A simple basic authentication middleware. Allows setup authentication using configuration or dependency injection and suppress WWW-Authenticate header globally or for AJAX request.

Installing

Install using the ZNetCS.AspNetCore.Authentication.Basic NuGet package

PM> Install-Package ZNetCS.AspNetCore.Authentication.Basic

Version history

6.0.0

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.

5.0.0

Added direct references to latest framework and removed no longer supported frameworks. Added possibility to suppress WWWAuthenticate header globally not only on Ajax request.

4.0.0

From now assembly is signed.

3.0.0

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.

Usage

When you install the package, it should be added to your .csproj. Alternatively, you can add it directly by adding:

<ItemGroup>
 <PackageReference Include="ZNetCS.AspNetCore.Authentication.Basic" Version="9.0.0" />
</ItemGroup>
using ZNetCS.AspNetCore.Authentication.Basic;
using ZNetCS.AspNetCore.Authentication.Basic.Events;
...

.NET 6

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 = new BasicAuthenticationEvents
 {
 OnValidatePrincipal = context =>
 {
 if ((context.UserName == "userName") && (context.Password == "password"))
 {
 var claims = new List<Claim>
 {
 new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
 };

 var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
 context.Principal = principal;
 }
 else 
 {
 // optional with following default.
 // context.AuthenticationFailMessage = "Authentication failed."; 
 }

 return Task.CompletedTask;
 }
 };
 });

or using dependency injection

public class AuthenticationEvents : BasicAuthenticationEvents
{
 #region Public Methods

 /// <inheritdoc/>
 public override Task ValidatePrincipalAsync(ValidatePrincipalContext context)
 {
 if ((context.UserName == "userName") && (context.Password == "password"))
 {
 var claims = new List<Claim>
 {
 new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
 };

 var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
 context.Principal = principal;
 }

 return Task.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 initialization
app.UseAuthentication();

// other middleware e.g. MVC etc

.NET 5 and Below

In 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).

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{ 

 // default authentication initialization
 app.UseAuthentication();

 // other middleware e.g. MVC etc
}

public void ConfigureServices(IServiceCollection services)
{
 services
 .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
 .AddBasicAuthentication(
 options =>
 {
 options.Realm = "My Application";
 options.Events = new BasicAuthenticationEvents
 {
 OnValidatePrincipal = context =>
 {
 if ((context.UserName == "userName") && (context.Password == "password"))
 {
 var claims = new List<Claim>
 {
 new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
 };

 var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
 context.Principal = principal;
 }
 else 
 {
 // optional with following default.
 // context.AuthenticationFailMessage = "Authentication failed."; 
 }

 return Task.CompletedTask;
 }
 };
 });
}

or using dependency injection:

public class AuthenticationEvents : BasicAuthenticationEvents
{
 #region Public Methods

 /// <inheritdoc/>
 public override Task ValidatePrincipalAsync(ValidatePrincipalContext context)
 {
 if ((context.UserName == "userName") && (context.Password == "password"))
 {
 var claims = new List<Claim>
 {
 new Claim(ClaimTypes.Name, context.UserName, context.Options.ClaimsIssuer)
 };

 var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
 context.Principal = principal;
 }

 return Task.CompletedTask;
 }

 #endregion
}

and then registration

public void ConfigureServices(IServiceCollection services)
{
 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.

public void ConfigureServices(IServiceCollection services)
{
 services.AddScoped<AuthenticationEvents>();

 services
 .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
 .AddBasicAuthentication(
 options =>
 {
 options.Realm = "My Application";
 options.AjaxRequestOptions.SuppressWwwAuthenticateHeader = true;
 });
}
Product Versions Compatible and additional computed target framework versions.
.NET net6.0 net6.0 is compatible.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 is compatible.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 is compatible.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ZNetCS.AspNetCore.Authentication.Basic:

Package Downloads
Substratum

Opinionated, production-grade application framework built on ASP.NET Core Minimal APIs. Authentication, authorization, database, caching, logging, OpenAPI docs, cloud storage, and real-time events — all pre-wired and configured via appsettings.json.

Etrmn.AuthenticationMiddleware

Remove cokkie props

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.0 22,588 11/29/2025
9.0.0 158,293 12/15/2024
6.0.1 358,177 1/27/2022
5.0.0 160,695 11/26/2020
4.0.0 164,862 1/27/2020
3.0.2 32,408 10/11/2019
3.0.1 89,965 4/9/2019
3.0.0 162,603 1/20/2018
2.0.0 18,495 8/29/2017
1.0.0 3,489 2/27/2017

Added support for .net core 10, drop support for .net 3.1 and 5.0