public static IdentityBuilder AddIdentityMongoDbProvider <TUser, TRole>(this IServiceCollection services,
                                                                                Action <IdentityOptions> setupIdentityAction, Action <MongoIdentityOptions> setupDatabaseAction) where TUser : MongoUser
            where TRole : MongoRole
        {
            var dbOptions = new MongoIdentityOptions();

            setupDatabaseAction(dbOptions);

            var builder = services.AddIdentity <TUser, TRole>(setupIdentityAction ?? (x => { }));

            builder.AddRoleStore <RoleStore <TRole> >()
            .AddUserStore <UserStore <TUser, TRole> >()
            .AddUserManager <UserManager <TUser> >()
            .AddRoleManager <RoleManager <TRole> >()
            .AddDefaultTokenProviders();


            var userCollection = MongoUtil.FromConnectionString <TUser>(dbOptions.ConnectionString, dbOptions.UsersCollection);
            var roleCollection = MongoUtil.FromConnectionString <TRole>(dbOptions.ConnectionString, dbOptions.RolesCollection);

            services.AddSingleton <IMongoCollection <TUser> >(x => userCollection);
            services.AddSingleton <IMongoCollection <TRole> >(x => roleCollection);

            // Identity Services
            services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole>(roleCollection));
            services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole>(userCollection, new RoleStore <TRole>(roleCollection), x.GetService <ILookupNormalizer>()));



            return(builder);
        }
Example #2
0
        public static IdentityBuilder AddMongoDbStores <TUser, TRole, TKey>(this IdentityBuilder builder, Action <MongoIdentityOptions> setupDatabaseAction,
                                                                            IdentityErrorDescriber identityErrorDescriber = null)
            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);
            var migrationUserCollection = MongoUtil.FromConnectionString <MigrationMongoUser <TKey> >(dbOptions, dbOptions.UsersCollection);
            var userCollection          = MongoUtil.FromConnectionString <TUser>(dbOptions, dbOptions.UsersCollection);
            var roleCollection          = MongoUtil.FromConnectionString <TRole>(dbOptions, dbOptions.RolesCollection);

            Migrator.Apply <MigrationMongoUser <TKey>, TRole, TKey>(migrationCollection, migrationUserCollection, roleCollection);

            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, identityErrorDescriber));
            builder.Services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole, TKey>(userCollection, roleCollection, identityErrorDescriber));

            return(builder);
        }
        public static IdentityBuilder AddIdentityMongoDbProvider <TUser, TRole, TKey>(this IServiceCollection services,
                                                                                      Action <IdentityOptions> setupIdentityAction, Action <MongoIdentityOptions> setupDatabaseAction, IdentityErrorDescriber identityErrorDescriber = null)
            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);
            var migrationUserCollection = MongoUtil.FromConnectionString <MigrationMongoUser <TKey> >(dbOptions, dbOptions.UsersCollection);
            var userCollection          = MongoUtil.FromConnectionString <TUser>(dbOptions, dbOptions.UsersCollection);
            var roleCollection          = MongoUtil.FromConnectionString <TRole>(dbOptions, dbOptions.RolesCollection);

            // apply migrations before identity services resolved
            Migrator.Apply <MigrationMongoUser <TKey>, TRole, TKey>(migrationCollection, migrationUserCollection, roleCollection);

            var builder = services.AddIdentity <TUser, TRole>(setupIdentityAction ?? (x => { }));

            builder.AddRoleStore <RoleStore <TRole, TKey> >()
            .AddUserStore <UserStore <TUser, TRole, TKey> >()
            .AddUserManager <UserManager <TUser> >()
            .AddRoleManager <RoleManager <TRole> >()
            .AddDefaultTokenProviders();

            services.AddSingleton(x => userCollection);
            services.AddSingleton(x => roleCollection);

            // register custom ObjectId TypeConverter
            if (typeof(TKey) == typeof(ObjectId))
            {
                TypeConverterResolver.RegisterTypeConverter <ObjectId, ObjectIdConverter>();
            }

            // Identity Services
            services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole, TKey>(roleCollection, identityErrorDescriber));
            services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole, TKey>(userCollection, roleCollection, identityErrorDescriber));

            return(builder);
        }
        public static IdentityBuilder AddIdentityMongoDbProvider <TUser, TRole, TKey>(this IServiceCollection services,
                                                                                      Action <IdentityOptions> setupIdentityAction, Action <MongoIdentityOptions> setupDatabaseAction)
            where TKey : IEquatable <TKey>
            where TUser : MongoUser <TKey>
            where TRole : MongoRole <TKey>
        {
            var dbOptions = new MongoIdentityOptions();

            setupDatabaseAction(dbOptions);

            var builder = services.AddIdentity <TUser, TRole>(setupIdentityAction ?? (x => { }));

            builder.AddRoleStore <RoleStore <TRole, TKey> >()
            .AddUserStore <UserStore <TUser, TRole, TKey> >()
            .AddUserManager <UserManager <TUser> >()
            .AddRoleManager <RoleManager <TRole> >()
            .AddDefaultTokenProviders();

            var migrationCollection = MongoUtil.FromConnectionString <MigrationHistory>(dbOptions.ConnectionString, dbOptions.MigrationCollection);

            Task.WaitAny(Migrator.Apply(migrationCollection));

            var userCollection = MongoUtil.FromConnectionString <TUser>(dbOptions.ConnectionString, dbOptions.UsersCollection);
            var roleCollection = MongoUtil.FromConnectionString <TRole>(dbOptions.ConnectionString, dbOptions.RolesCollection);

            services.AddSingleton(x => userCollection);
            services.AddSingleton(x => roleCollection);

            // register custom ObjectId TypeConverter
            if (typeof(TKey) == typeof(ObjectId))
            {
                RegisterTypeConverter <ObjectId, ObjectIdConverter>();
            }

            // Identity Services
            services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole, TKey>(roleCollection));
            services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole, TKey>(userCollection, new RoleStore <TRole, TKey>(roleCollection), x.GetService <ILookupNormalizer>()));

            return(builder);
        }