Example #1
0
        /// <summary>
        /// Adds mongodb implementation for the "Asp Net Core Identity" part (saving user and roles)
        /// </summary>
        /// <remarks><![CDATA[
        /// Contains implemenations for
        /// - IUserStore<T>
        /// - IRoleStore<T>
        /// ]]></remarks>
        public static IIdentityServerBuilder AddMongoDbForAspIdentity <TIdentity, TRole>(this IIdentityServerBuilder builder, IConfigurationRoot configurationRoot) where
        TIdentity : Microsoft.AspNetCore.Identity.MongoDB.IdentityUser where TRole : Microsoft.AspNetCore.Identity.MongoDB.IdentityRole
        {
            //User Mongodb for Asp.net identity in order to get users stored
            var configurationOptions = configurationRoot.Get <ConfigurationOptions>();
            var client   = new MongoClient(configurationOptions.MongoConnection);
            var database = client.GetDatabase(configurationOptions.MongoDatabaseName);



            // Configure Asp Net Core Identity / Role to use MongoDB
            builder.Services.AddSingleton <IUserStore <TIdentity> >(x =>
            {
                var usersCollection = database.GetCollection <TIdentity>("Identity_Users");
                IndexChecks.EnsureUniqueIndexOnNormalizedEmail(usersCollection);
                IndexChecks.EnsureUniqueIndexOnNormalizedUserName(usersCollection);
                return(new UserStore <TIdentity>(usersCollection));
            });

            builder.Services.AddSingleton <IRoleStore <TRole> >(x =>
            {
                var rolesCollection = database.GetCollection <TRole>("Identity_Roles");
                IndexChecks.EnsureUniqueIndexOnNormalizedRoleName(rolesCollection);
                return(new RoleStore <TRole>(rolesCollection));
            });
            builder.Services.AddIdentity <TIdentity, TRole>();


            return(builder);
        }
        public MongoRoleStore(IMongoDatabase database)
        {
            var rolesCollection = database.GetCollection <WrappedIdentityRole>("Identity_Roles");

            IndexChecks.EnsureUniqueIndexOnNormalizedRoleName(rolesCollection);

            innerStore = new RoleStore <WrappedIdentityRole>(rolesCollection);
        }
Example #3
0
        public static IIdentityServerBuilder AddMongoDbForAspIdentity <TIdentity, TRole>(this IIdentityServerBuilder builder, IConfiguration configuration)
            where TIdentity : External.IdentityUser where TRole : External.IdentityRole
        {
            var configurationOptions = configuration.Get <ConfigurationOptions>();
            // TODO: Inject connectionString and dbName
            var client = new MongoClient(
                //configurationOptions.MongoConnection
                "mongodb://localhost:27017"
                );
            var database = client.GetDatabase(
                //configurationOptions.MongoDatabaseName
                "testDatabase"
                );

            // Changed to return UserStore instead of interface IUserStore to work
            builder.Services.AddSingleton <IUserStore <TIdentity> >(x =>
            {
                var usersCollection = database.GetCollection <TIdentity>("Identity_Users");
                IndexChecks.EnsureUniqueIndexOnNormalizedEmail(usersCollection);
                IndexChecks.EnsureUniqueIndexOnNormalizedUserName(usersCollection);
                return(new External.UserStore <TIdentity>(usersCollection));
            });

            // Changed to return RoleStore instead of interface IRoleStore to work
            builder.Services.AddSingleton <IRoleStore <TRole> >(x =>
            {
                var rolesCollection = database.GetCollection <TRole>("Identity_Roles");
                IndexChecks.EnsureUniqueIndexOnNormalizedRoleName(rolesCollection);
                return(new RoleStore <TRole>(rolesCollection));
            });

            // TODO: Add password settings, etc.
            // https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?tabs=aspnetcore2x
            builder.Services.AddIdentity <TIdentity, TRole>()
            .AddUserStore <UserStore <TIdentity> >()
            .AddRoleStore <RoleStore <TRole> >()
            .AddDefaultTokenProviders();
            //.AddOpenIddict();

            return(builder);
        }
        public static IdentityBuilder AddMongoStores <TRepository>(this IdentityBuilder builder)
            where TRepository : IMongoIdentityRepository
        {
            var provider = builder.Services.BuildServiceProvider();
            var repo     = (TRepository)provider.GetService <IMongoIdentityRepository>();

            builder.Services.AddSingleton <IUserStore <AppUser> >(p =>
            {
                IndexChecks.EnsureUniqueIndexOnNormalizedEmail(
                    repo.GetDatabase().GetCollection <AppUser>(AppIndentityConstants.Mongo.IdentityUserCollectionName));
                IndexChecks.EnsureUniqueIndexOnNormalizedUserName(
                    repo.GetDatabase().GetCollection <AppUser>(AppIndentityConstants.Mongo.IdentityUserCollectionName));
                return(new AppUserStore <AppUser>(repo));
            });
            builder.Services.AddSingleton <IRoleStore <AppRole> >(p =>
            {
                IndexChecks.EnsureUniqueIndexOnNormalizedRoleName(
                    repo.GetDatabase().GetCollection <AppRole>(AppIndentityConstants.Mongo.IdentityRoleCollectionName));
                return(new AppRoleStore <AppRole>(repo));
            });
            return(builder);
        }
        public static IdentityBuilder AddMongoIdentityStores <TIdentity, TRole>(this IServiceCollection services, IMongoDatabase database) where TIdentity : IdentityUser where TRole : IdentityRole
        {
            services.AddSingleton <IUserStore <TIdentity> >(x =>
            {
                IMongoCollection <TIdentity> usersCollection = database.GetCollection <TIdentity>("Identity_Users");

                IndexChecks.EnsureUniqueIndexOnNormalizedEmail(usersCollection);
                IndexChecks.EnsureUniqueIndexOnNormalizedUserName(usersCollection);

                return(new UserStore <TIdentity>(usersCollection));
            });

            services.AddSingleton <IRoleStore <TRole> >(x =>
            {
                var rolesCollection = database.GetCollection <TRole>("Identity_Roles");

                IndexChecks.EnsureUniqueIndexOnNormalizedRoleName(rolesCollection);

                return(new RoleStore <TRole>(rolesCollection));
            });

            return(services.AddIdentity <TIdentity, TRole>());
        }
Example #6
0
        protected override void Load(ContainerBuilder builder)
        {
            var databaseName = Configuration.GetValue <string>("squidex:stores:mongoDb:databaseName");

            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ConfigurationException("You must specify the MongoDB database name in the 'squidex:stores:mongoDb:databaseName' configuration section.");
            }

            var connectionString = Configuration.GetValue <string>("squidex:stores:mongoDb:connectionString");

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ConfigurationException("You must specify the MongoDB connection string in the 'squidex:stores:mongoDb:connectionString' configuration section.");
            }

            var databaseNameContent = Configuration.GetValue <string>("squidex:stores:mongoDb:databaseNameContent");

            if (string.IsNullOrWhiteSpace(databaseNameContent))
            {
                databaseNameContent = databaseName;
            }

            builder.Register(c => new MongoClient(connectionString))
            .As <IMongoClient>()
            .SingleInstance();

            builder.Register(c => c.Resolve <IMongoClient>().GetDatabase(databaseName))
            .Named <IMongoDatabase>(MongoDatabaseName)
            .SingleInstance();

            builder.Register(c => c.Resolve <IMongoClient>().GetDatabase(databaseNameContent))
            .Named <IMongoDatabase>(MongoDatabaseNameContent)
            .SingleInstance();

            builder.Register <IUserStore <IdentityUser> >(c =>
            {
                var usersCollection = c.ResolveNamed <IMongoDatabase>(MongoDatabaseName).GetCollection <IdentityUser>("Identity_Users");

                IndexChecks.EnsureUniqueIndexOnNormalizedEmail(usersCollection);
                IndexChecks.EnsureUniqueIndexOnNormalizedUserName(usersCollection);

                return(new UserStore <IdentityUser>(usersCollection));
            })
            .SingleInstance();

            builder.Register <IRoleStore <IdentityRole> >(c =>
            {
                var rolesCollection = c.ResolveNamed <IMongoDatabase>(MongoDatabaseName).GetCollection <IdentityRole>("Identity_Roles");

                IndexChecks.EnsureUniqueIndexOnNormalizedRoleName(rolesCollection);

                return(new RoleStore <IdentityRole>(rolesCollection));
            })
            .SingleInstance();

            builder.RegisterType <MongoUserRepository>()
            .As <IUserRepository>()
            .InstancePerLifetimeScope();

            builder.RegisterType <MongoPersistedGrantStore>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseName))
            .As <IPersistedGrantStore>()
            .SingleInstance();

            builder.RegisterType <MongoEventConsumerInfoRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseName))
            .As <IEventConsumerInfoRepository>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoContentRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseNameContent))
            .As <IContentRepository>()
            .As <IEventConsumer>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoHistoryEventRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseName))
            .As <IHistoryEventRepository>()
            .As <IEventConsumer>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoSchemaRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseName))
            .As <ISchemaRepository>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoAppRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseName))
            .As <IAppRepository>()
            .As <IEventConsumer>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.Register(c =>
                             new CompoundEventConsumer(
                                 c.Resolve <MongoSchemaRepository>(),
                                 c.Resolve <CachingSchemaProvider>()))
            .As <IEventConsumer>()
            .AsSelf()
            .SingleInstance();

            builder.Register(c =>
                             new CompoundEventConsumer(
                                 c.Resolve <MongoAppRepository>(),
                                 c.Resolve <CachingAppProvider>()))
            .As <IEventConsumer>()
            .AsSelf()
            .SingleInstance();
        }