WebFlow 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package WebFlow --version 1.0.0
NuGet\Install-Package WebFlow -Version 1.0.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="WebFlow" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WebFlow --version 1.0.0
#r "nuget: WebFlow, 1.0.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.
// Install WebFlow as a Cake Addin
#addin nuget:?package=WebFlow&version=1.0.0

// Install WebFlow as a Cake Tool
#tool nuget:?package=WebFlow&version=1.0.0

WebFlow

A SDK for ASP.Net making development easier

Extra

If for any reason the documenation below isn't clear we have a fully working example within WebFlowTest

Project Documentation

This document provides an overview of two essential interfaces and their implementations within the project.

WebFlowAuthorizationService Documentation

This document provides an overview of the IWebFlowAuthorizationService interface and related components within the project.

IWebFlowAuthorizationService

The IWebFlowAuthorizationService interface manages user authorization within the web flow.

Methods

RegisterUser<T>(DbContext dbContext, T authenticationObject)

Registers a user in the database. Passwords are automatically hashed when provided.

Result<T?> RegisterUser<T>(DbContext dbContext, T authenticationObject) where T : class;
AuthenticateUserAsync<T>(DbContext dbContext, HttpContext httpContext, T authenticationObject)

Authenticates the user based on provided attributes.

Task<Result<T?>> AuthenticateUserAsync<T>(DbContext dbContext, HttpContext httpContext, T authenticationObject) where T : class;
LogoutUser(HttpContext httpContext)

Logs the user out and invalidates their session.

Result LogoutUser(HttpContext httpContext);

Configuration Utilities

RegisterAuthorizationService(IServiceCollection serviceCollection, Assembly executing, JwtConfig jwtConfig)

Registers the authorization service based on provided configurations.

public static void RegisterAuthorizationService(IServiceCollection serviceCollection, Assembly executing, JwtConfig jwtConfig);
RegisterAuthorizationMiddlewares(IApplicationBuilder applicationBuilder, AuthorizationType authorizationType)

Registers middlewares for authorization handling.

public static void RegisterAuthorizationMiddlewares(IApplicationBuilder applicationBuilder, AuthorizationType authorizationType);

Attributes

AuthenticationClaimAttribute

Issues a claim in the httpContext.

[AuthenticationClaim("UserId")]
public Guid Id { get; set; }
AuthenticationFieldAttribute

Specifies that this value is checked upon authentication.

[AuthenticationField]
public required string EmailAddress { get; set; }
PasswordAttribute

Specifies that the value is a password and needs to be hashed and checked upon authentication.

[Password(HashType.PBKDF2)]
public required string Password { get; set; }
UniqueAttribute

Specifies that the value must not be a duplicate within the database.

[Unique]
public required string EmailAddress { get; set; }

Usage Examples

Here are snippets showcasing the usage of the IWebFlowAuthorizationService interface and related attributes:

Interface Usage

public class User : IEntityTypeConfiguration<User>
{
    [AuthenticationClaim("UserId")] public Guid Id { get; set; }

    [Unique, AuthenticationClaim("EmailAddress"), AuthenticationField]
    public required string EmailAddress { get; set; }

    [Password(HashType.PBKDF2)] 
    public required string Password { get; set; }

    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.Property(x => x.Id)
            .HasDefaultValue(Guid.NewGuid());
    }
}

[AdaptTo(typeof(User))]
public record AuthorizationRequest(string EmailAddress, string Password)
{
    public static explicit operator User(AuthorizationRequest user) =>
        user.Adapt<User>();
}

[HttpPost("create")]
public async Task<IActionResult> CreateUser(AuthorizationRequest authorizationRequest)
{
    await using var context = await _dbContext.CreateDbContextAsync();

    Result<User?> result = _authorizationService.RegisterUser(context, (User)authorizationRequest);
    if (!result.IsSuccess)
        return BadRequest();
        
    await context.SaveChangesAsync();
        
    var cachedUser = (CachedUser)result.Unwrap()!;
    _genericCacheService.CacheObject(cachedUser);
        
    return Ok(result.Unwrap());
}

[HttpPost("login")]
public async Task<IActionResult> LoginUser(AuthorizationRequest authorizationRequest)
{
    await using var context = await _dbContext.CreateDbContextAsync();

    Result<User?> result = await _authorizationService.AuthenticateUserAsync(context, HttpContext, (User)authorizationRequest);
    if (!result.IsSuccess)
        return BadRequest();
        
    await context.SaveChangesAsync();

    return Ok();
}

IGenericCacheService

This interface manages caching operations for various objects.

Methods

CacheObject<T>(T genericObject, TimeSpan? expiry = null, When when = When.Always)

Caches an object into a Redis database.

void CacheObject<T>(T genericObject, TimeSpan? expiry = null, When when = When.Always);
FetchAll(Type genericObject)

Fetches all cached variations of a given type.

List<string> FetchAll(Type genericObject);
FetchObject(Type genericObject, string key)

Fetches a cached object based on a provided key.

string? FetchObject(Type genericObject, string key);
FetchNearest(Type genericObject, string guess)

Fetches an item based on the nearest guess to the cache key.

List<string> FetchNearest(Type genericObject, string guess);
UpdateObject<T>(T genericObject)

Updates the item within the cache.

void UpdateObject<T>(T genericObject);
DeleteObject(string key)

Deletes an item from the cache.

bool DeleteObject(string key);
RefreshCacheAsync(List<Type> genericObjects)

Refreshes the cache with the provided set of objects.

Task RefreshCacheAsync(List<Type> genericObjects);

Usage Examples

Here are snippets showcasing the usage of these interfaces:

User Authorization

// Creating a user
Result<User?> result = _authorizationService.RegisterUser(context, (User)authorizationRequest);

// Logging in a user
Result<User?> result = await _authorizationService.AuthenticateUserAsync(context, HttpContext, (User)authorizationRequest);

// Logging out a user
_authorizationService.LogoutUser(HttpContext);

Caching Operations

// Caching an object
_genericCacheService.CacheObject(cachedUser);

// Fetching a cached object
_genericCacheService.FetchObject(typeof(CachedUser), userId.ToString());

// Fetching all cached variations
_genericCacheService.FetchAll(typeof(CachedUser));

// Deleting an object from the cache
_genericCacheService.DeleteObject(key);

// Refreshing the cache
await _genericCacheService.RefreshCacheAsync(new List<Type> { typeof(CachedUser) });

Model

[AdaptFrom(typeof(User))]
public class CachedUser
{
    [CacheKey]
    public Guid Id { get; set; }

    public required string EmailAddress { get; set; }

    public static explicit operator CachedUser(User user) =>
        user.Adapt<CachedUser>();
}
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. 
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.3 88 2/19/2024
1.2.2 95 2/13/2024
1.2.1 91 1/20/2024
1.2.0 136 12/3/2023
1.1.0 126 11/27/2023
1.0.0 96 11/21/2023