Skip to main content
.NETFramework4.7.2

Package Description

Published
May 26, 2024
2y ago
Total Downloads
1.7K
1,738

Install

dotnet add package Unravel.AspNet.Identity.EntityFramework --version 0.1.36-alpha
ProductVersions
Other
.NETFramework4.7.2

README

Unravel.AspNet.Identity.EntityFramework

Unravel provides AddIdentity() extension methods on IServiceCollection. The resulting IdentityBuilder provides methods and an extension point to configure Microsoft.AspNet.Identity services:

  • AddEntityFrameworkStores<TContext>() registers a context inheriting from IdentityDbContext<>, plus dependent store implementations
    • Calls SetPerOwinContext<T>(), from Unravel.AspNet.Identity, replacing CreatePerOwinContext<T>()

This is designed to directly migrate from the scaffolded Startup.Auth.cs pattern:

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context, user manager and signin manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    // ...
}

To:

public override void ConfigureServices(IServiceCollection services)
{
    services.AddIdentity<ApplicationUser>()
        .AddEntityFrameworkStores(ApplicationDbContext.Create)
        .AddUserManager<ApplicationUserManager>(ApplicationUserManager.Create)
        .AddSignInManager<ApplicationSignInManager>(ApplicationSignInManager.Create)
}

You can then refactor away from the Create methods to constructor injection, e.g.

--- a/App_Start/IdentityConfig.cs
+++ b/App_Start/IdentityConfig.cs
@@ -12,2 +12,3 @@
 using Microsoft.Owin.Security;
+using Microsoft.Owin.Security.DataProtection;
 using UnravelExamples.Identity.Models;
@@ -37,10 +38,10 @@
     {
-        public ApplicationUserManager(IUserStore<ApplicationUser> store)
+        public ApplicationUserManager(IUserStore<ApplicationUser> store, IDataProtectionProvider dataProtectionProvider)
             : base(store)
         {
+            Init(this, dataProtectionProvider);
         }

-        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
+        private static ApplicationUserManager Init(ApplicationUserManager manager, IDataProtectionProvider dataProtectionProvider)
         {
-            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
             // Configure validation logic for usernames
@@ -80,3 +81,2 @@
             manager.SmsService = new SmsService();
-            var dataProtectionProvider = options.DataProtectionProvider;
             if (dataProtectionProvider != null)
--- a/App_Start/Startup.Auth.cs
+++ b/App_Start/Startup.Auth.cs
@@ -20,5 +20,5 @@
             services.AddIdentity<ApplicationUser>()
-                .AddEntityFrameworkStores(ApplicationDbContext.Create)
-                .AddUserManager<ApplicationUserManager>(ApplicationUserManager.Create)
-                .AddSignInManager<ApplicationSignInManager>(ApplicationSignInManager.Create)
+                .AddEntityFrameworkStores<ApplicationDbContext>()
+                .AddUserManager<ApplicationUserManager>()
+                .AddSignInManager<ApplicationSignInManager>()
                 ;

Changes from Previous Version

No structural changes between v0.1.30-alpha and v0.1.36-alpha.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.