SquareWidget.BasicAuth.Core 1.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package SquareWidget.BasicAuth.Core --version 1.0.0
NuGet\Install-Package SquareWidget.BasicAuth.Core -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="SquareWidget.BasicAuth.Core" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SquareWidget.BasicAuth.Core --version 1.0.0
#r "nuget: SquareWidget.BasicAuth.Core, 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 SquareWidget.BasicAuth.Core as a Cake Addin
#addin nuget:?package=SquareWidget.BasicAuth.Core&version=1.0.0

// Install SquareWidget.BasicAuth.Core as a Cake Tool
#tool nuget:?package=SquareWidget.BasicAuth.Core&version=1.0.0

SquareWidget.BasicAuthentication.Core

Yet another basic authentication handler for ASP.NET Core 2.1. Supports IdentityServer4 or a database.

Getting Started

Register in Startup

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddMvcCore()
            .AddAuthorization();

        // If you want to authenticate against IdentityServer4
        var authenticationUrl = "https://identityserver.io";
        var scope = "api1";

        // If you want to authenticate against a database
        var connectionString = "Server=tcp:serverName;Database=db;User ID=uid;Password=pwd";

        services
            .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
            .AddBasicAuthentication<BasicAuthenticationService>(o =>
            {
                o.DiscoveryUrl = authenticationUrl;
                o.Scope = scope;
                o.ConnectionString = connectionString;
            });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // other settings

        app.UseAuthentication();
        app.UseMvc();
    }
}

You have to implement a BasicAuthenticationService in your code. To call a discovery endpoint on IdentityServer4:

public class BasicAuthenticationService : IBasicAuthenticationService
{
    public Task<bool> IsValidUserAsync(BasicAuthenticationOptions options, string username, string password)
    {
        var discoveryClient = new DiscoveryClient(options.DiscoveryUrl);
        var discoveryResponse = discoveryClient.GetAsync().Result;
        if (discoveryResponse.IsError)
        {
            return Task.FromResult(false);
        }

        var tokenClient = new TokenClient(discoveryResponse.TokenEndpoint, username, password);
        var tokenResponse = tokenClient.RequestClientCredentialsAsync(options.Scope).Result;
        if (tokenResponse.IsError)
        {
            return Task.FromResult(false);
        }

        return Task.FromResult(true);
    }
}

Or maybe you want to initialize a repository class that knows how to call a database:

public class BasicAuthenticationService : IBasicAuthenticationService
{
    public Task<bool> IsValidUserAsync(BasicAuthenticationOptions options, string username, string password)
    {
        var repository = new UserRepository(options.ConnectionString);
        return Task.FromResult(repository.IsValidUser(username, password));
    }                       
}

Versioning

Version 1.0 targeting ASP.NET Core 2.1

Authors

James Still

License

This project is licensed under the MIT License.

Acknowledgments

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,641 11/5/2018