Fluent.NHibernate.AspNetCore.Identity 1.0.0

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

// Install Fluent.NHibernate.AspNetCore.Identity as a Cake Tool
#tool nuget:?package=Fluent.NHibernate.AspNetCore.Identity&version=1.0.0

FluentNHibernate.AspNetCore.Identity

Highly customizable ASP.NET Core Identity provider for NHibernate with FluentNHibernate mapping support.

<a href="https://www.nuget.org/packages/Fluent.NHibernate.AspNetCore.Identity/absoluteLatest"> <img alt="Nuget (with prereleases)" src="https://img.shields.io/nuget/vpre/Fluent.NHibernate.AspNetCore.Identity"> </a>

1. Getting started

Install the package

PM> Install-Package Fluent.NHibernate.AspNetCore.Identity

Add the default entity mappings (only when not using custom entities)

using FluentNHibernate.AspNetCore.Identity;

Fluently.Configure()
    .Database(persistenceConfigurer)
    .Mappings(t =>
    {
        // Only use this method when there are no custom identity entities.
        t.FluentMappings.AddIdentityMappings();
    }) 
    // ...

Register the NHibernate stores

using FluentNHibernate.AspNetCore.Identity;

services.AddScoped(t => SessionFactory.OpenSession()) // ISession required for resolving store services.

services.AddIdentityCore<IdentityUser>()
    .AddRoles<IdentityRole>() // Optional
    .AddNHibernateStores(t => t.SetSessionAutoFlush(false));

2. Custom entities/mappings

Define custom entity

using FluentNHibernate.AspNetCore.Identity;

public class ApplicationUser : IdentityUser<string> // Configurable PK type
{
    public virtual DateTime BirthDate { get; set; } // Custom properties
}

Define custom mapping

using FluentNHibernate.AspNetCore.Identity;

public class ApplicationUserMap : IdentityUserMapBase<ApplicationUser, string>
{
    public ApplicationUserMap() : base(t => t.GeneratedBy.TriggerIdentity().Length(32)) // Primary key config
    {
        Map(t => t.BirthDate).Not.Nullable(); // Custom property map
    }
}

Add custom entity mappings

using FluentNHibernate.AspNetCore.Identity;

Fluently.Configure()
    .Database(persistenceConfigurer)
    .Mappings(t =>
    {
        // Add your custom mappings.
        t.FluentMappings.AddFromAssembly(/* Your custom mappings assembly */))
        // Manually add the default mappings for non-customized entities
        .Add<IdentityRoleClaimMap>()
        .Add<IdentityUserLoginMap>()
        // ...
    }) 

NOTE: Do not use the FluentMappings.AddIdentityMappings() method when there are custom identity entities defined. Mappings for default entities must be added manually since calling AddIdentityMappings() would add unnecessary mappings for base types of custom identity entities that derive from those types.

Register the NHibernate stores for custom entities

using FluentNHibernate.AspNetCore.Identity;

services.AddScoped(t => SessionFactory.OpenSession()) // ISession required for resolving store services.

services.AddIdentityCore<ApplicationUser>()
    .AddRoles<ApplicationRoles>()
    .ExtendConfiguration()
    .AddUserClaim<ApplicationUserClaim>()
    .AddUserToken<ApplicationUserToken>()
    // Other custom entities...
    .AddNHibernateStores(t => t.SetSessionAutoFlush(true));

NOTE: Identity entities omitted during service registration (IdentityUserClaim<Tkey>, IdentityUserToken<TKey>, etc.) are automatically registered with the default type and the TKey generic argument representing the entity's primary key is inferred from the registered IdentityUser<TKey> type. E.g. registered IdentityUser<long> will automatically cause a default IdentityUserToken<long> to be registered, when this specific entity registration is omitted.

3. Extra options

i. Overriding schema/table names

a. Override the default schema or table names by constructor parameter.
using FluentNHibernate.AspNetCore.Identity;

public class ApplicationUserMap : IdentityUserMapBase<ApplicationUser, string>
{
    public ApplicationUserMap() : base("MyTableName", t => t.GeneratedBy.TriggerIdentity().Length(32))
    {
        // Custom property maps...
    }
}

// OR

public class ApplicationUserMap : IdentityUserMapBase<ApplicationUser, string>
{
    public ApplicationUserMap() : base("MySchemaName", "MyTableName", t => t.GeneratedBy.TriggerIdentity().Length(32))
    {
        // Custom property maps...
    }
}
b. Override the default schema or table names by calling configuration methods from the base map class.
using FluentNHibernate.AspNetCore.Identity;

public class ApplicationUserMap : IdentityUserMapBase<ApplicationUser, string>
{
    public ApplicationUserMap() : base(t => t.GeneratedBy.TriggerIdentity().Length(32))
    {
        Schema("MySchemaName");
        Table("MyTableName");
    }
}

Default schema name: "dbo" (SQL Server default)

ii. Controlling session auto flush in store services

By default, after calling create, update or delete methods in the identity stores, the NHibernate session is automatically flushed.

The auto flush behavior can be controlled through the SetSessionAutoFlush(bool) method.

services.AddIdentityCore<ApplicationUser>()
    // ...
    .AddNHibernateStores(t => t.SetSessionAutoFlush(false));

Default: true

iii. Controlling store GUID format

The identity store automatically generate GUIDs for determined string properties (e.g. ConcurrencyStamp). The format of the GUID's string representation can be defined through the SetGuidFormat(GuidFormat) method.

services.AddIdentityCore<ApplicationUser>()
    // ...
    .AddNHibernateStores(t => t.SetGuidFormat(GuidFormat.Digits));

Default: GuidFormat.Hyphens ("D")

iv. Adding custom identity entities through extended configuration

Custom identity entities (apart from IdentityUser<TKey> and IdentityRole<TKey>) can be defined during store service registration through the ExtendConfiguration method.

services.AddIdentityCore<ApplicationUser>()
    .AddRoles<ApplicationRoles>()
    // -----------------------------------
    .ExtendConfiguration()
    .AddUserClaim<ApplicationUserClaim>()
    .AddUserToken<ApplicationUserToken>()
    // Other custom entities...
    // -----------------------------------
    .AddNHibernateStores(t => t.SetSessionAutoFlush(true));

4. Usage example

using FluentNHibernate.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity;

public class RegistrationController
{
    private readonly IUserStore<IdentityUser> _userStore; // Or your custom IdentityUser type that derives from IdentityUser<TKey>

    public RegistrationController(IUserStore<IdentityUser> userStore)
    {
        _userStore = userStore;
    }

    // ...
}

5. License

MIT License (MIT)

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.

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.0.0 16,296 2/28/2022
1.0.0-alpha.1 130 1/24/2022

| 1.0.0 - Stable Release |

Breaking Changes:
- [NHibernateStoreOptions] SetAutoFlushSession renamed to SetSessionAutoFlush.
- [AddNHibernateStores] configureStoreOptions parameter is now an Action<NHibernateStoreOptions> instead of Func<NHibernateStoreOptions,NHibernateStoreOptions>.