ASP.NET Core 5.0 - SingleUser NuGet Package
Update 07/29/2021
I updated KenHaggerty.Com.SingleUser NuGet package to support Bootstrap v5. I updated the SingleUser Class Lib Project and the article with Bootstrap v5 markup. The Login page Implements a Bootstrap checkbox with both V4 and V5 checkbox classes.
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
- Install KenHaggerty.Com.SingleUser.
- Edit the host web app's Startup.cs.
- Authorize the Admin folder.
- Add the _SingleUserLoginPartial to the host's Shared/_Layout.cshtml's navbar.
- Build, run, and test.
Install KenHaggerty.Com.SingleUser.
- Manage NuGet Packages on the host.
- Search for kenhaggerty on the Browse tab.
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.UseSingleUser();
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.UseSingleUser();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
Edit the host Shared/_Layout.cshtml
- Insert _SingleUserLoginPartial into the host's navbar.
navbar-collapse with flex-sm-row-reverse
<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>
navbar-collapse with justify-content-between
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<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>
<partial name="_SingleUserLoginPartial" />
</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;
});
UseSingleUser Implements:
app.UseAuthentication();
_SingleUserLoginPartial.cshtml Implements:
<ul class="navbar-nav">
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<form class="d-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>
The Login page Implements a Bootstrap checkbox with both V4 and V5 checkbox classes.
Login.cshtml:
<div class="custom-control custom-checkbox form-check">
<input type="checkbox" class="custom-control-input form-check-input" asp-for="IsPersistent" />
<label class="custom-control-label form-check-label" asp-for="IsPersistent">
@Html.DisplayNameFor(model => model.IsPersistent)
</label>
</div>
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.
Comments(0)