joelbyford.BasicAuth 1.2.0

dotnet add package joelbyford.BasicAuth --version 1.2.0
                    
NuGet\Install-Package joelbyford.BasicAuth -Version 1.2.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="joelbyford.BasicAuth" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="joelbyford.BasicAuth" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="joelbyford.BasicAuth" />
                    
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 joelbyford.BasicAuth --version 1.2.0
                    
#r "nuget: joelbyford.BasicAuth, 1.2.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 joelbyford.BasicAuth@1.2.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=joelbyford.BasicAuth&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=joelbyford.BasicAuth&version=1.2.0
                    
Install as a Cake Tool

Code Modifications

Once installed, to use the library, simply modify the Configure method in your startup.cs to call the library in any one of two ways:

Authorize a Single User

For simple use cases, this may satisfy your need. PLEASE take steps to avoid having credentials in code

using joelbyford;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
        string basicAuthRealm = "mywebsite.com";
        string basicAuthUser = "testUser"; //hardcoded values here for example only
        string basicAuthPass = "testPass"; //hardcoded values here for example only
        app.UseMiddleware<joelbyford.BasicAuth>(basicAuthRealm, basicAuthUser, basicAuthPass);
    }

Authorize a Dictionary of Users

If you would like to control how and where you get the users and passwords from, this method is best (e.g. you are obtaining from a database). PLEASE take steps to avoid having credentials in code

using joelbyford;
using System.IO;
using System.Text.Json;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
        Dictionary<string, string> myUsers = new Dictionary<string, string>();
        var packageJson = File.ReadAllText("authorizedUsers.json");
        myUsers = JsonSerializer.Deserialize<Dictionary<string, string>>(packageJson);
        string basicAuthRealm = "mywebsite.com";
        app.UseMiddleware<joelbyford.BasicAuth>(basicAuthRealm, myUsers);
    }

In this example, a json file is loaded from the web app's root directory with the following format:

{    
    "testUser" : "testPassword",
    "devUser" : "devPassword"
}

This can of course be loaded in from a database call instead as long as users and passwords are loaded into a Dictionary<string, string>

To see an example of this in use, please see startup.cs in the https://github.com/joelbyford/DepCalcsCS.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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.2.0 4,933 5/19/2022
1.1.1 2,245 1/18/2022
1.1.0 544 8/24/2021