Dzaba.BasicAuthentication 1.0.0

dotnet add package Dzaba.BasicAuthentication --version 1.0.0                
NuGet\Install-Package Dzaba.BasicAuthentication -Version 1.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="Dzaba.BasicAuthentication" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dzaba.BasicAuthentication --version 1.0.0                
#r "nuget: Dzaba.BasicAuthentication, 1.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.
// Install Dzaba.BasicAuthentication as a Cake Addin
#addin nuget:?package=Dzaba.BasicAuthentication&version=1.0.0

// Install Dzaba.BasicAuthentication as a Cake Tool
#tool nuget:?package=Dzaba.BasicAuthentication&version=1.0.0                

Dzaba Basic Authentication handler

Usage

Install nuget https://www.nuget.org/packages/Dzaba.BasicAuthentication/

In Startup or Program.cs:

var builder = WebApplication.CreateBuilder(args);

// ...

builder.Services.AddBasicAuthentication<MyHandler>();

builder.Services.AddAuthentication(o =>
{
    o.AddBasicAuthenticationScheme(true);
});

builder.Services.AddAuthorization();

Where MyHandler is your custom implementation. Example:

internal sealed class MyHandler : IBasicAuthenticationHandlerService
{
    private readonly IMyUserService userService;

    public MyHandler(IMyUserService userService)
    {
        this.userService = userService;
    }

    public async Task AddClaimsAsync(BasicAuthenticationCredentials credentials, HttpContext httpContext, ICollection<Claim> claims, object context)
    {
        var userModel = (UserModel)context;

        await foreach (var role in userService.GetRolesAsync(userModel))
        {
            claims.Add(new Claim(ClaimTypes.Role, role));
        }
    }

    public async Task<CheckPasswordResult> CheckPasswordAsync(BasicAuthenticationCredentials credentials, HttpContext httpContext)
    {
        var userModel = await userService.GetUserModelAsync(credentials.UserName);
        if (userModel == null)
        {
            return new CheckPasswordResult("Invalid user name.");
        }

        if (await userService.IsPasswordOkAsync(userModel, credentials.Password))
        {
            return CheckPasswordResult.Success(userModel);
        }

        return new CheckPasswordResult("Invalid user name or password.");
    }

    public async Task HandleUnauthorizedAsync(HttpContext httpContext, string failReason)
    {
        await httpContext.Response.WriteAsync(failReason).ConfigureAwait(false);
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dzaba.BasicAuthentication:

Package Downloads
Dzaba.PathoAutho.Client

PathoAutho client library and handler for ASP.NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 133 3/3/2024