ASP.NET Core 5.0 - SingleUser NuGet Package

Ken Haggerty
Created 10/17/2020 - Updated 10/17/2020 18:08
ASP.NET Core 5.0.RC2 requires Visual Studio 2019 v16.8.0 Preview 4 or above.
KenHaggerty.Com.SingleUser v1.0.1 verified on NET 5.0.RC2.
I will update the project with new NET 5 releases.

This article will describe the KenHaggerty.Com.SingleUser NuGet package. The package provides cookie authentication for a single user to an ASP.NET Core Web Application. I will assume you have created a new ASP.NET Core 5.0 Razor Pages project. See Tutorial: Get started with Razor Pages in ASP.NET Core.

The KenHaggerty.Com.SingleUser NuGet package is the result of a web app I developed. It was a simple web app which had Log In, Log Out, and Admin pages. I used a hard-coded user and cookie authentication to restrict access to Admin pages. Copying the pages into new projects was easier than copying the project and renaming the namespaces. I realized implementing authentication for a single user was the perfect candidate for my first published NuGet package at KenHaggerty.Com.SingleUser. This article will focus on implementing the KenHaggerty.Com.SingleUser published package.

Quick Start

  1. Install KenHaggerty.Com.SingleUser.
  2. Edit the host web app's Startup.cs.
  3. Authorize the Admin folder.
  4. Add the _SingleUserLoginPartial to the host's Shared/_Layout.cshtml's navbar.
  5. Build, run, and test.
Install KenHaggerty.Com.SingleUser.
  • Manage NuGet Packages on the host.
  • Check Include prerelease.
  • Search for kenhaggerty on the Browse tab.
KenHaggerty.Com.SingleUser NuGet Package
Edit the host Startup.ConfigureServices
  • Authorize the Admin folder.
  • Add services.AddSingleUser();
public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages(options => { options.Conventions.AuthorizeFolder("/Admin"); });
    services.AddSingleUser();
}
Edit the host Startup.Configure
  • Add app.UseSingleUserMiddleware();
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseSingleUserMiddleware();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
    });
}
Edit the host Shared/_Layout.cshtml
  • Insert _SingleUserLoginPartial into the host's navbar.
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
    <partial name="_SingleUserLoginPartial" />
    <ul class="navbar-nav flex-grow-1">
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="" asp-page="/Admin/Index">Admin</a>
        </li>
    </ul>
</div>
Package Log In and Log Out Pages
  • Located in an Account folder/path.
  • Reference the host's Shared/_Layout.cshtml.
  • Log In page verifies configuration.
Default Admin/Index Page
  • Located in an Admin folder/path.
  • References the host's Shared/_Layout.cshtml.
  • Configuration hints and verification.
  • Overridden by the host's Admin/Index page.
Password (Not Required)

For basic password support, add a SingleUserPassword key with your password to the host's appsettings.json's root.

AddSingleUser Implements:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = "/account/login";
        options.LogoutPath = "/account/logout";
        options.Cookie.SameSite = SameSiteMode.Strict;
        options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
        options.Cookie.IsEssential = true;
    });

UseSingleUserMiddleware Implements:

app.UseAuthentication();

_SingleUserLoginPartial.cshtml Implements:

<ul class="navbar-nav">
    @@if (User.Identity.IsAuthenticated)
    {
        <li class="nav-item">
            <form class="form-inline" asp-page="/Account/LogOut" method="post">
                <button type="submit" class="nav-link text-dark btn btn-link pt-1">
                    Log Out @@User.Identity.Name
                </button>
            </form>
        </li>
    }
    else
    {
        <li class="nav-item">
            <a class="nav-link text-dark" asp-page="/Account/LogIn">Log In</a>
        </li>
    }
</ul>

Registered users on KenHaggerty.Com can download the source code for free at Manage > Assets. I created a topic, ASP.NET Core 5.0 - SingleUser Class Lib Project for discussions. More details and screenshots at ASP.NET Core 5.0 - SingleUser Class Lib Project. Coming soon, an article series about implementing a Razor Class Library.

Article Tags:

Authorization

Comment Count = 0

Please log in to comment.

Login Register
Logged in users receive web notifications.
Web Notifications