SquareWidget.BasicAuth.Core 1.0.0

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

Install-Package SquareWidget.BasicAuth.Core -Version 1.0.0
dotnet add package SquareWidget.BasicAuth.Core --version 1.0.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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

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

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 378 11/5/2018