Schick.Authentication.OneTimeToken.Abstractions 1.2.1

dotnet add package Schick.Authentication.OneTimeToken.Abstractions --version 1.2.1
NuGet\Install-Package Schick.Authentication.OneTimeToken.Abstractions -Version 1.2.1
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="Schick.Authentication.OneTimeToken.Abstractions" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Schick.Authentication.OneTimeToken.Abstractions --version 1.2.1
#r "nuget: Schick.Authentication.OneTimeToken.Abstractions, 1.2.1"
#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 Schick.Authentication.OneTimeToken.Abstractions as a Cake Addin
#addin nuget:?package=Schick.Authentication.OneTimeToken.Abstractions&version=1.2.1

// Install Schick.Authentication.OneTimeToken.Abstractions as a Cake Tool
#tool nuget:?package=Schick.Authentication.OneTimeToken.Abstractions&version=1.2.1

ASP.NET One-Time Token Authentication

Provides an authentication scheme for one-time access tokens to support file download via direct link for protected resources

Frameworks supported

  • ASP.NET Core 6.0
  • ASP.NET Core 7.0

Demo

A demo application can be found in folder FS.Authentication.OneTimeToken.Demo

Installation

Install from NuGet package

Install-Package Schick.Authentication.OneTimeToken

Getting Started

Add Authorize attribute with one-time token authentication scheme to controller / minimal API

// Authenticate via one-time access token.
[Authorize(AuthenticationSchemes = OneTimeTokenDefaults.AuthenticationScheme)]
internal static string RequireOneTimeTokenAuthentication()
	=> "Hello, you are authenticated";

Register one-time access token authorization scheme

// Add default authentication, e.g. NTLM/Windows, JWT, ...
builder.Services.AddAuthentication(...);
...
// Add one-time access token authentication.
builder.Services.AddOneTimeTokenAuthentication(config => { /* your options here */ });

Tokens can be generated via IOneTimeTokenService.CreateToken

<serviceProvider>.GetRequiredService<IOneTimeTokenService>().CreateToken(/* claims */);

Full Sample

using FS.Authentication.OneTimeToken.Abstractions.Interfaces;
using FS.Authentication.OneTimeToken.Abstractions.Models;
using FS.Authentication.OneTimeToken.Extensions;
using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Annotations;
using System.ComponentModel;

namespace FS.Authentication.OneTimeToken.Demo;

public class Program
{
    public const string DEFAULT_ROLE = "DefaultUser";

    // Authenticate / authorize via default authentication, e.g. NTLM/Windows, JWT, ...
    [Authorize]
    internal static string GetOneTimeToken(HttpContext httpContext, [FromQuery][DefaultValue(DEFAULT_ROLE)][SwaggerParameter(Required = false)] string role)
        => httpContext.RequestServices.GetRequiredService<IOneTimeTokenService>().CreateToken(role);

    // Authenticate via one-time access token.
    [Authorize(AuthenticationSchemes = OneTimeTokenDefaults.AuthenticationScheme)]
    internal static string RequireOneTimeTokenAuthentication([FromQuery] string accessToken)
        => "Hello, you are authenticated";

    // Authenticate /authorize via one-time access token.
    [Authorize(AuthenticationSchemes = OneTimeTokenDefaults.AuthenticationScheme, Roles = DEFAULT_ROLE)]
    internal static string RequireOneTimeTokenAuthorization([FromQuery] string accessToken)
        => "Hello, you are authorized";

    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Add default authentication, e.g. NTLM/Windows, JWT, ...
        builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
        builder.Services.AddAuthorization();

        // Add one-time access token authentication.
        builder.Services.AddOneTimeTokenAuthentication(config => { /* your options here */ });

        AddSwagger(builder);

        var app = builder.Build();

        app.UseAuthentication();
        app.UseAuthorization();

        app.MapGet(nameof(GetOneTimeToken), GetOneTimeToken);
        app.MapGet(nameof(RequireOneTimeTokenAuthentication), RequireOneTimeTokenAuthentication);
        app.MapGet(nameof(RequireOneTimeTokenAuthorization), RequireOneTimeTokenAuthorization);

        AddSwaggerUi(app);

        app.Run();
    }

    private static void AddSwagger(WebApplicationBuilder builder)
    {
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen(config => config.EnableAnnotations());
    }

    private static void AddSwaggerUi(WebApplication app)
    {
        app.UseSwagger();
        app.UseSwaggerUI(options =>
        {
            options.DisplayRequestDuration();
            options.EnableDeepLinking();
            options.EnableTryItOutByDefault();
            options.ConfigObject.AdditionalItems.Add("requestSnippetsEnabled", true);
        });
    }
}
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.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Schick.Authentication.OneTimeToken.Abstractions:

Package Downloads
Schick.Authentication.OneTimeToken

ASP.NET Core one-time authentication token services

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.1 416 6/17/2023
1.2.0 121 6/16/2023
1.1.0 245 5/13/2023
1.0.2 476 2/18/2023
1.0.1 368 12/24/2022
1.0.0 346 11/25/2022
1.0.0-alpha1 115 11/25/2022