From 11982179b41b8913d4835f68552e009168a19e0a Mon Sep 17 00:00:00 2001 From: vova3211 Date: Tue, 22 Jun 2021 13:36:48 +0300 Subject: [PATCH] added AddMongoDbStores extension to register only stores. --- .../Mongo/TypeConverterResolver.cs | 18 ++++++ .../MongoIdentityExtensions.cs | 10 +--- .../MongoStoreExtensions.cs | 55 +++++++++++++++++++ 3 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/AspNetCore.Identity.Mongo/Mongo/TypeConverterResolver.cs create mode 100644 src/AspNetCore.Identity.Mongo/MongoStoreExtensions.cs diff --git a/src/AspNetCore.Identity.Mongo/Mongo/TypeConverterResolver.cs b/src/AspNetCore.Identity.Mongo/Mongo/TypeConverterResolver.cs new file mode 100644 index 0000000..85a90b9 --- /dev/null +++ b/src/AspNetCore.Identity.Mongo/Mongo/TypeConverterResolver.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; + +namespace AspNetCore.Identity.Mongo.Mongo +{ + internal static class TypeConverterResolver + { + internal static void RegisterTypeConverter() where TC : TypeConverter + { + Attribute[] attr = new Attribute[1]; + TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); + attr[0] = vConv; + TypeDescriptor.AddAttributes(typeof(T), attr); + } + } +} diff --git a/src/AspNetCore.Identity.Mongo/MongoIdentityExtensions.cs b/src/AspNetCore.Identity.Mongo/MongoIdentityExtensions.cs index 901c4f0..e33ad0b 100644 --- a/src/AspNetCore.Identity.Mongo/MongoIdentityExtensions.cs +++ b/src/AspNetCore.Identity.Mongo/MongoIdentityExtensions.cs @@ -100,7 +100,7 @@ public static IdentityBuilder AddIdentityMongoDbProvider(thi // register custom ObjectId TypeConverter if (typeof(TKey) == typeof(ObjectId)) { - RegisterTypeConverter(); + TypeConverterResolver.RegisterTypeConverter(); } // Identity Services @@ -109,13 +109,5 @@ public static IdentityBuilder AddIdentityMongoDbProvider(thi return builder; } - - private static void RegisterTypeConverter() where TC : TypeConverter - { - Attribute[] attr = new Attribute[1]; - TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); - attr[0] = vConv; - TypeDescriptor.AddAttributes(typeof(T), attr); - } } } \ No newline at end of file diff --git a/src/AspNetCore.Identity.Mongo/MongoStoreExtensions.cs b/src/AspNetCore.Identity.Mongo/MongoStoreExtensions.cs new file mode 100644 index 0000000..a880a9e --- /dev/null +++ b/src/AspNetCore.Identity.Mongo/MongoStoreExtensions.cs @@ -0,0 +1,55 @@ +using AspNetCore.Identity.Mongo.Migrations; +using AspNetCore.Identity.Mongo.Model; +using AspNetCore.Identity.Mongo.Mongo; +using AspNetCore.Identity.Mongo.Stores; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection; +using MongoDB.Bson; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Text; +using System.Threading.Tasks; + +namespace AspNetCore.Identity.Mongo +{ + public static class MongoStoreExtensions + { + public static IdentityBuilder AddMongoDbStores(this IdentityBuilder builder, Action setupDatabaseAction) + where TUser : MongoUser + { + return AddMongoDbStores(builder, setupDatabaseAction); + } + + public static IdentityBuilder AddMongoDbStores(this IdentityBuilder builder, Action setupDatabaseAction) + where TKey : IEquatable + where TUser : MongoUser + where TRole : MongoRole + { + var dbOptions = new MongoIdentityOptions(); + setupDatabaseAction(dbOptions); + + var migrationCollection = MongoUtil.FromConnectionString(dbOptions, dbOptions.MigrationCollection); + + Task.WaitAny(Migrator.Apply(migrationCollection)); + + var userCollection = MongoUtil.FromConnectionString(dbOptions, dbOptions.UsersCollection); + var roleCollection = MongoUtil.FromConnectionString(dbOptions, dbOptions.RolesCollection); + + builder.Services.AddSingleton(x => userCollection); + builder.Services.AddSingleton(x => roleCollection); + + // register custom ObjectId TypeConverter + if (typeof(TKey) == typeof(ObjectId)) + { + TypeConverterResolver.RegisterTypeConverter(); + } + + // Identity Services + builder.Services.AddTransient>(x => new RoleStore(roleCollection)); + builder.Services.AddTransient>(x => new UserStore(userCollection, new RoleStore(roleCollection), x.GetService())); + + return builder; + } + } +}