forked from matteofabbri/AspNetCore.Identity.Mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request matteofabbri#101 from matteofabbri/MongoStoreExten…
…sion AddMongoDbStores extension to register only stores.
- Loading branch information
Showing
3 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/AspNetCore.Identity.Mongo/Mongo/TypeConverterResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T, TC>() where TC : TypeConverter | ||
{ | ||
Attribute[] attr = new Attribute[1]; | ||
TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC)); | ||
attr[0] = vConv; | ||
TypeDescriptor.AddAttributes(typeof(T), attr); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TUser>(this IdentityBuilder builder, Action<MongoIdentityOptions> setupDatabaseAction) | ||
where TUser : MongoUser | ||
{ | ||
return AddMongoDbStores<TUser, MongoRole, ObjectId>(builder, setupDatabaseAction); | ||
} | ||
|
||
public static IdentityBuilder AddMongoDbStores<TUser, TRole, TKey>(this IdentityBuilder builder, Action<MongoIdentityOptions> setupDatabaseAction) | ||
where TKey : IEquatable<TKey> | ||
where TUser : MongoUser<TKey> | ||
where TRole : MongoRole<TKey> | ||
{ | ||
var dbOptions = new MongoIdentityOptions(); | ||
setupDatabaseAction(dbOptions); | ||
|
||
var migrationCollection = MongoUtil.FromConnectionString<MigrationHistory>(dbOptions, dbOptions.MigrationCollection); | ||
|
||
Task.WaitAny(Migrator.Apply(migrationCollection)); | ||
|
||
var userCollection = MongoUtil.FromConnectionString<TUser>(dbOptions, dbOptions.UsersCollection); | ||
var roleCollection = MongoUtil.FromConnectionString<TRole>(dbOptions, dbOptions.RolesCollection); | ||
|
||
builder.Services.AddSingleton(x => userCollection); | ||
builder.Services.AddSingleton(x => roleCollection); | ||
|
||
// register custom ObjectId TypeConverter | ||
if (typeof(TKey) == typeof(ObjectId)) | ||
{ | ||
TypeConverterResolver.RegisterTypeConverter<ObjectId, ObjectIdConverter>(); | ||
} | ||
|
||
// Identity Services | ||
builder.Services.AddTransient<IRoleStore<TRole>>(x => new RoleStore<TRole, TKey>(roleCollection)); | ||
builder.Services.AddTransient<IUserStore<TUser>>(x => new UserStore<TUser, TRole, TKey>(userCollection, new RoleStore<TRole, TKey>(roleCollection), x.GetService<ILookupNormalizer>())); | ||
|
||
return builder; | ||
} | ||
} | ||
} |