public static IMongoCollection <TItem> FromConnectionString <TItem>(MongoIdentityOptions options, string collectionName)
        {
            IMongoCollection <TItem> collection;

            var type = typeof(TItem);


            if (options.ConnectionString != null)
            {
                var url      = new MongoUrl(options.ConnectionString);
                var settings = MongoClientSettings.FromUrl(url);

                settings.SslSettings         = options.SslSettings;
                settings.ClusterConfigurator = options.ClusterConfigurator;

                var client = new MongoClient(settings);
                collection = client.GetDatabase(url.DatabaseName ?? "default")
                             .GetCollection <TItem>(collectionName ?? type.Name.ToLowerInvariant());
            }
            else
            {
                var settings = new MongoClientSettings();

                settings.SslSettings         = options.SslSettings;
                settings.ClusterConfigurator = options.ClusterConfigurator;

                collection = new MongoClient(settings).GetDatabase("default")
                             .GetCollection <TItem>(collectionName ?? type.Name.ToLowerInvariant());
            }

            return(collection);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityTestBase{T}"/> class.
        /// </summary>
        /// <param name="testFixture">The application test fixture.</param>
        protected SecurityTestBase(AppTestFixture <Startup> testFixture)
            : base(testFixture)
        {
            Environment.SetEnvironmentVariable("GOOGLE_CLIENT_ID", this.Faker.Random.AlphaNumeric(10));
            Environment.SetEnvironmentVariable("GOOGLE_SECRET", this.Faker.Random.AlphaNumeric(10));
            Environment.SetEnvironmentVariable("FACEBOOK_CLIENT_ID", this.Faker.Random.AlphaNumeric(10));
            Environment.SetEnvironmentVariable("FACEBOOK_SECRET", this.Faker.Random.AlphaNumeric(10));

            testFixture?.ConfigureTestServices(services =>
            {
                // Setup user manager and role manager for integration test overriding the default implementation.
                var databaseOptions = new MongoIdentityOptions
                {
                    ConnectionString = testFixture.RepositoryConfiguration.ConnectionString
                };

                var userCollection = MongoUtil.FromConnectionString <UserAccount>(databaseOptions.ConnectionString, databaseOptions.UsersCollection);
                var roleCollection = MongoUtil.FromConnectionString <UserRole>(databaseOptions.ConnectionString, databaseOptions.RolesCollection);

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

                services.AddTransient <IUserStore <UserAccount> >(x =>
                                                                  new UserStore <UserAccount, UserRole>(
                                                                      userCollection,
                                                                      new RoleStore <UserRole>(roleCollection),
                                                                      x.GetService <ILookupNormalizer>()));

                services.AddTransient <IRoleStore <UserRole> >(x => new RoleStore <UserRole>(roleCollection));
            });
        }
Beispiel #3
0
        public static IdentityBuilder AddAdditionalIdentityMongoDbProvider <TUser, TRole>(this IServiceCollection services,
                                                                                          Action <IdentityOptions> setupIdentityAction, Action <MongoIdentityOptions> setupDatabaseAction) where TUser : MongoUser
            where TRole : MongoRole
        {
            var dbOptions = new MongoIdentityOptions();

            setupDatabaseAction(dbOptions);

            services.TryAddTransient <RoleStore <TRole> >();
            services.TryAddTransient <UserStore <TUser, TRole> >();
            services.TryAddTransient <UserManager <TUser> >();
            services.TryAddTransient <RoleManager <TRole> >();
            services.TryAddScoped <SignInManager <TUser> >();

            services.TryAddScoped <IUserValidator <TUser>, UserValidator <TUser> >();
            services.TryAddScoped <IPasswordValidator <TUser>, PasswordValidator <TUser> >();
            services.TryAddScoped <IPasswordHasher <TUser>, PasswordHasher <TUser> >();
            services.TryAddScoped <IRoleValidator <TRole>, RoleValidator <TRole> >();
            services.TryAddScoped <IUserClaimsPrincipalFactory <TUser>, UserClaimsPrincipalFactory <TUser, TRole> >();

            var userCollection = new IdentityUserCollection <TUser>(dbOptions.ConnectionString, dbOptions.UsersCollection);
            var roleCollection = new IdentityRoleCollection <TRole>(dbOptions.ConnectionString, dbOptions.RolesCollection);

            services.AddTransient <IIdentityUserCollection <TUser> >(x => userCollection);
            services.AddTransient <IIdentityRoleCollection <TRole> >(x => roleCollection);

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

            if (setupIdentityAction != null)
            {
                services.Configure(setupIdentityAction);
            }
            var builder = new IdentityBuilder(typeof(TUser), typeof(TRole), services);

            builder.AddDefaultTokenProviders();
            return(builder);
        }
Beispiel #4
0
        public static IdentityBuilder RegisterMongoIdentity <TUser, TRole>(this IServiceCollection services,
                                                                           Action <IdentityOptions> setupIdentityOptions, Action <MongoIdentityOptions> setupMongoOptions)
            where TUser : ApplicationUser
            where TRole : IdentityRole
        {
            var dbOptions = new MongoIdentityOptions();

            setupMongoOptions(dbOptions);

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

            builder.AddUserStore <UserStore <TUser, TRole> >()
            .AddRoleStore <RoleStore <TRole> >()
            .AddDefaultTokenProviders();

            var userRepository = new UserRepository(dbOptions.ConnectionString, dbOptions.DbName);
            var roleRepository = new RoleRepository(dbOptions.ConnectionString, dbOptions.DbName);

            services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole>(userRepository));
            services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole>(roleRepository));

            return(builder);
        }