예제 #1
0
        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> >()
            .AddDefaultTokenProviders();


            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));


            return(builder);
        }
예제 #2
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);
        }
        public static IdentityBuilder AddIdentityMongoDbProvider <TUser, TRole>(this IServiceCollection services,
                                                                                Action <IdentityOptions> setupIdentityAction, Action <MongoIdentityOptions> setupDatabaseAction) where TUser : MongoUser
            where TRole : MongoRole
        {
            MongoIdentityOptions dbOptions = new MongoIdentityOptions();

            setupDatabaseAction(dbOptions);

            IdentityOptions identityOptions = new IdentityOptions();

            identityOptions.Password.RequireDigit           = false;
            identityOptions.Password.RequireLowercase       = false;
            identityOptions.Password.RequireNonAlphanumeric = false;
            identityOptions.Password.RequireUppercase       = false;
            identityOptions.Password.RequiredLength         = 6;
            identityOptions.Password.RequiredUniqueChars    = 0;
            setupIdentityAction = new Action <IdentityOptions>(x => x.Password = identityOptions.Password);
            IdentityBuilder builder = services.AddIdentity <TUser, TRole>(setupIdentityAction ?? (x => { }));

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


            IdentityUserCollection <TUser> userCollection = new IdentityUserCollection <TUser>(dbOptions.ConnectionString, dbOptions.UsersCollection);
            IdentityRoleCollection <TRole> 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));


            return(builder);
        }