/// <summary>
 /// Add a custom implementation for <see cref="UsersTable{TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken}"/>.
 /// </summary>
 /// <typeparam name="TUsersTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role and user.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUsersTable <TUsersTable, TUser, TKey>(this DapperStoreOptions options)
     where TUsersTable : UsersTable <TUser, TKey, IdentityUserClaim <TKey>, IdentityUserRole <TKey>, IdentityUserLogin <TKey>, IdentityUserToken <TKey>, IdentityRole <TKey> >
     where TUser : IdentityUser <TKey>
     where TKey : IEquatable <TKey>
 {
     options.AddUsersTable <TUsersTable, TUser, TKey, IdentityUserClaim <TKey>, IdentityUserRole <TKey>, IdentityUserLogin <TKey>, IdentityUserToken <TKey>, IdentityRole <TKey> >();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UserClaimsTable{TKey, TUserClaim}"/>.
 /// </summary>
 /// <typeparam name="TUserClaimsTable">The type of the table to register.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a user.</typeparam>
 /// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserClaimsTable <TUserClaimsTable, TKey, TUserClaim>(this DapperStoreOptions options)
     where TUserClaimsTable : UserClaimsTable <TKey, TUserClaim>
     where TKey : IEquatable <TKey>
     where TUserClaim : IdentityUserClaim <TKey>, new()
 {
     options.Services.AddScoped(typeof(IUserClaimsTable <,>).MakeGenericType(typeof(TKey), typeof(TUserClaim)), typeof(TUserClaimsTable));
 }
 /// <summary>
 /// Add a custom implementation for <see cref="RolesTable{TRole, TKey, TRoleClaim}"/>.
 /// </summary>
 /// <typeparam name="TRolesTable">The type of the table to register.</typeparam>
 /// <typeparam name="TRole">The type of the class representing a role.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role.</typeparam>
 /// <typeparam name="TRoleClaim">The type of the class representing a role claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddRolesTable <TRolesTable, TRole, TKey, TRoleClaim>(this DapperStoreOptions options)
     where TRolesTable : RolesTable <TRole, TKey, TRoleClaim>
     where TRole : IdentityRole <TKey>
     where TKey : IEquatable <TKey>
     where TRoleClaim : IdentityRoleClaim <TKey>, new()
 {
     options.Services.AddScoped(typeof(IRolesTable <, ,>).MakeGenericType(typeof(TRole), typeof(TKey), typeof(TRoleClaim)), typeof(TRolesTable));
 }
示例#4
0
        private static void AddStores(IServiceCollection services, Type userType, Type roleType, Action <DapperStoreOptions> configureAction = null)
        {
            var identityUserType = FindGenericBaseType(userType, typeof(IdentityUser <>));

            if (identityUserType == null)
            {
                throw new InvalidOperationException($"Method {nameof(AddDapperStores)} can only be called with a user that derives from IdentityUser<TKey>.");
            }
            var serviceProvider = services.BuildServiceProvider();
            var configuration   = serviceProvider.GetRequiredService <IConfiguration>();

            var dbConnectionContextOptions = new DapperStoreOptions
            {
                ConnectionString    = configuration.GetConnectionString("DefaultConnection"),
                DbConnectionFactory = new SqlServerDbConnectionFactory(),
                Services            = services
            };

            configureAction?.Invoke(dbConnectionContextOptions);
            dbConnectionContextOptions.Services = null;

            var keyType = identityUserType.GenericTypeArguments[0];

            services.TryAddScoped(typeof(IDbConnectionFactory), x =>
            {
                var dbConnectionFactoryInstance = (IDbConnectionFactory)Activator.CreateInstance(dbConnectionContextOptions.DbConnectionFactory.GetType());
                dbConnectionFactoryInstance.ConnectionString = dbConnectionContextOptions.ConnectionString;
                return(dbConnectionFactoryInstance);
            });

            Type userStoreType;
            var  userClaimType = typeof(IdentityUserClaim <>).MakeGenericType(keyType);
            var  userRoleType  = typeof(IdentityUserRole <>).MakeGenericType(keyType);
            var  userLoginType = typeof(IdentityUserLogin <>).MakeGenericType(keyType);
            var  roleClaimType = typeof(IdentityRoleClaim <>).MakeGenericType(keyType);
            var  userTokenType = typeof(IdentityUserToken <>).MakeGenericType(keyType);

            if (roleType != null)
            {
                var identityRoleType = FindGenericBaseType(roleType, typeof(IdentityRole <>));
                if (identityRoleType == null)
                {
                    throw new InvalidOperationException($"Method {nameof(AddDapperStores)} can only be called with a role that derives from IdentityRole<TKey>.");
                }
                services.TryAddScoped(
                    typeof(IUsersTable <, , , , , ,>).MakeGenericType(userType, keyType, userClaimType, userRoleType, userLoginType, userTokenType, roleType),
                    typeof(UsersTable <, , , , , ,>).MakeGenericType(userType, keyType, userClaimType, userRoleType, userLoginType, userTokenType, roleType)
                    );
                services.TryAddScoped(typeof(IRolesTable <, ,>).MakeGenericType(roleType, keyType, roleClaimType), typeof(RolesTable <, ,>).MakeGenericType(roleType, keyType, roleClaimType));
                services.TryAddScoped(typeof(IUserRolesTable <, ,>).MakeGenericType(roleType, keyType, userRoleType), typeof(UserRolesTable <, ,>).MakeGenericType(roleType, keyType, userRoleType));
                services.TryAddScoped(typeof(IRoleClaimsTable <,>).MakeGenericType(keyType, roleClaimType), typeof(RoleClaimsTable <,>).MakeGenericType(keyType, roleClaimType));
                services.TryAddScoped(typeof(IRoleStore <>).MakeGenericType(roleType), typeof(RoleStore <, , ,>).MakeGenericType(roleType, keyType, userRoleType, roleClaimType));
                userStoreType = typeof(UserStore <, , , , , , ,>).MakeGenericType(userType, roleType, keyType, userClaimType, userRoleType, userLoginType, userTokenType, roleClaimType);
            }
            else
            {
                services.TryAddScoped(
                    typeof(IUsersOnlyTable <, , , , ,>).MakeGenericType(userType, keyType, userClaimType, userLoginType, userTokenType, roleType),
                    typeof(UsersTable <, , , , , ,>).MakeGenericType(userType, keyType, userClaimType, roleType, userLoginType, userTokenType, roleType)
                    );
                userStoreType = typeof(UserOnlyStore <, , , , ,>).MakeGenericType(userType, keyType, userClaimType, userLoginType, userTokenType);
            }
            services.TryAddScoped(typeof(IUserClaimsTable <,>).MakeGenericType(keyType, userClaimType), typeof(UserClaimsTable <,>).MakeGenericType(keyType, userClaimType));
            services.TryAddScoped(typeof(IUserLoginsTable <, ,>).MakeGenericType(userType, keyType, userLoginType), typeof(UserLoginsTable <, ,>).MakeGenericType(userType, keyType, userLoginType));
            services.TryAddScoped(typeof(IUserTokensTable <,>).MakeGenericType(keyType, userTokenType), typeof(UserTokensTable <,>).MakeGenericType(keyType, userTokenType));
            services.TryAddScoped(typeof(IUserStore <>).MakeGenericType(userType), userStoreType);
        }
 /// <summary>
 /// Add a custom implementation for <see cref="UserClaimsTable{TKey, TUserClaim}"/>.
 /// </summary>
 /// <typeparam name="TUserClaimsTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserClaimsTable <TUserClaimsTable, TUserClaim>(this DapperStoreOptions options)
     where TUserClaimsTable : UserClaimsTable <string, TUserClaim>
     where TUserClaim : IdentityUserClaim <string>, new()
 {
     options.AddUserClaimsTable <TUserClaimsTable, string, TUserClaim>();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="RolesTable{TRole, TKey, TRoleClaim}"/>.
 /// </summary>
 /// <typeparam name="TRolesTable">The type of the table to register.</typeparam>
 /// <typeparam name="TRole">The type of the class representing a role.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddRolesTable <TRolesTable, TRole>(this DapperStoreOptions options)
     where TRolesTable : RolesTable <TRole, string, IdentityRoleClaim <string> >
     where TRole : IdentityRole <string>
 {
     options.AddRolesTable <TRolesTable, TRole, string, IdentityRoleClaim <string> >();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UserTokensTable{TKey, TUserToken}"/>.
 /// </summary>
 /// <typeparam name="TUserTokensTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUserToken">The type representing a user token.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserTokensTable <TUserTokensTable, TUserToken>(this DapperStoreOptions options)
     where TUserTokensTable : UserTokensTable <string, TUserToken>
     where TUserToken : IdentityUserToken <string>, new()
 {
     options.AddUserTokensTable <TUserTokensTable, string, TUserToken>();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UsersTable{TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken}"/>.
 /// </summary>
 /// <typeparam name="TUsersTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a role and user.</typeparam>
 /// <typeparam name="TUserClaim">The type representing a claim.</typeparam>
 /// <typeparam name="TUserRole">The type representing a user role.</typeparam>
 /// <typeparam name="TUserLogin">The type representing a user external login.</typeparam>
 /// <typeparam name="TUserToken">The type representing a user token.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUsersTable <TUsersTable, TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRole>(this DapperStoreOptions options)
     where TUsersTable : UsersTable <TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRole>
     where TUser : IdentityUser <TKey>
     where TKey : IEquatable <TKey>
     where TUserClaim : IdentityUserClaim <TKey>, new()
     where TUserRole : IdentityUserRole <TKey>, new()
     where TUserLogin : IdentityUserLogin <TKey>, new()
     where TUserToken : IdentityUserToken <TKey>, new()
     where TRole : IdentityRole <TKey>, new()
 {
     options.Services.AddScoped(
         typeof(IUsersTable <, , , , , ,>).MakeGenericType(typeof(TUser), typeof(TKey), typeof(TUserClaim), typeof(TUserRole), typeof(TUserLogin), typeof(TUserToken), typeof(TRole)),
         typeof(TUsersTable));
 }
 /// <summary>
 /// Add a custom implementation for <see cref="RoleClaimsTable{TKey, TRoleClaim}"/>.
 /// </summary>
 /// <typeparam name="TRoleClaimsTable">The type of the table to register.</typeparam>
 /// <typeparam name="TRoleClaim">The type of the class representing a role claim.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddRoleClaimsTable <TRoleClaimsTable, TRoleClaim>(this DapperStoreOptions options)
     where TRoleClaimsTable : RoleClaimsTable <string, TRoleClaim>
     where TRoleClaim : IdentityRoleClaim <string>, new()
 {
     options.AddRoleClaimsTable <TRoleClaimsTable, string, TRoleClaim>();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UsersTable{TUser, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken}"/>.
 /// </summary>
 /// <typeparam name="TUsersTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUsersTable <TUsersTable, TUser>(this DapperStoreOptions options)
     where TUsersTable : UsersTable <TUser, string, IdentityUserClaim <string>, IdentityUserRole <string>, IdentityUserLogin <string>, IdentityUserToken <string>, IdentityRole <string> >
     where TUser : IdentityUser <string>
 {
     options.AddUsersTable <TUsersTable, TUser, string>();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UserRolesTable{TRole, TKey, TUserRole}"/>.
 /// </summary>
 /// <typeparam name="TUserRolesTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUserRole">The type representing a user role.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserRolesTable <TUserRolesTable, TUserRole>(this DapperStoreOptions options)
     where TUserRolesTable : UserRolesTable <IdentityRole, string, TUserRole>
     where TUserRole : IdentityUserRole <string>, new()
 {
     options.AddUserRolesTable <TUserRolesTable, IdentityRole, string, TUserRole>();
 }
 /// <summary>
 /// Add a custom implementation for <see cref="UserLoginsTable{TUser, TKey, TUserLogin}"/>.
 /// </summary>
 /// <typeparam name="TUserLoginsTable">The type of the table to register.</typeparam>
 /// <typeparam name="TUser">The type representing a user.</typeparam>
 /// <typeparam name="TKey">The type of the primary key for a user.</typeparam>
 /// <typeparam name="TUserLogin">The type representing a user external login.</typeparam>
 /// <param name="options">Options for configuring Dapper stores.</param>
 public static void AddUserLoginsTable <TUserLoginsTable, TUser, TKey, TUserLogin>(this DapperStoreOptions options)
     where TUserLoginsTable : UserLoginsTable <TUser, TKey, TUserLogin>
     where TUser : IdentityUser <TKey>
     where TKey : IEquatable <TKey>
     where TUserLogin : IdentityUserLogin <TKey>, new()
 {
     options.Services.AddScoped(typeof(IUserLoginsTable <, ,>).MakeGenericType(typeof(TUser), typeof(TKey), typeof(TUserLogin)), typeof(TUserLoginsTable));
 }