diff --git a/Daarto.sln b/Daarto.sln index 819ba7b..6238980 100644 --- a/Daarto.sln +++ b/Daarto.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29613.14 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32811.315 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AE94DD8D-1626-4618-AB38-37459DEE54E0}" EndProject @@ -13,8 +13,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{525F EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "database", "database", "{661AB817-BF00-4B7F-88B5-9ADC84DC93A5}" EndProject -Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "IdentityDb", "misc\database\IdentityDb\IdentityDb.sqlproj", "{F082EB24-7AA8-4ECA-9856-6375B7AAAECB}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Daarto.WebUI", "samples\Daarto.WebUI\Daarto.WebUI.csproj", "{22BA1F98-E1F9-421E-9B84-CAC07745BAEF}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "data", "data", "{C413D479-154F-4A24-9984-2B0D0C0C4CC2}" @@ -28,8 +26,11 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{B0C64769-72F4-42AE-B0E3-938FF8D1C145}" ProjectSection(SolutionItems) = preProject misc\docs\1.use_the_package.md = misc\docs\1.use_the_package.md + README.md = README.md EndProjectSection EndProject +Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "IdentityDatabase", "misc\database\IdentityDatabase\IdentityDatabase.sqlproj", "{EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -40,16 +41,16 @@ Global {DD01411B-826F-4CA3-8581-8FDA9C7961AC}.Debug|Any CPU.Build.0 = Debug|Any CPU {DD01411B-826F-4CA3-8581-8FDA9C7961AC}.Release|Any CPU.ActiveCfg = Release|Any CPU {DD01411B-826F-4CA3-8581-8FDA9C7961AC}.Release|Any CPU.Build.0 = Release|Any CPU - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB}.Debug|Any CPU.Deploy.0 = Debug|Any CPU - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB}.Release|Any CPU.Build.0 = Release|Any CPU - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB}.Release|Any CPU.Deploy.0 = Release|Any CPU {22BA1F98-E1F9-421E-9B84-CAC07745BAEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {22BA1F98-E1F9-421E-9B84-CAC07745BAEF}.Debug|Any CPU.Build.0 = Debug|Any CPU {22BA1F98-E1F9-421E-9B84-CAC07745BAEF}.Release|Any CPU.ActiveCfg = Release|Any CPU {22BA1F98-E1F9-421E-9B84-CAC07745BAEF}.Release|Any CPU.Build.0 = Release|Any CPU + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}.Release|Any CPU.Build.0 = Release|Any CPU + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC}.Release|Any CPU.Deploy.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -57,10 +58,10 @@ Global GlobalSection(NestedProjects) = preSolution {DD01411B-826F-4CA3-8581-8FDA9C7961AC} = {AE94DD8D-1626-4618-AB38-37459DEE54E0} {661AB817-BF00-4B7F-88B5-9ADC84DC93A5} = {AAE1B832-6032-4C5C-8154-F6775CC051F8} - {F082EB24-7AA8-4ECA-9856-6375B7AAAECB} = {661AB817-BF00-4B7F-88B5-9ADC84DC93A5} {22BA1F98-E1F9-421E-9B84-CAC07745BAEF} = {525F9EE4-17EB-4E98-A775-C28282BC2962} {C413D479-154F-4A24-9984-2B0D0C0C4CC2} = {AAE1B832-6032-4C5C-8154-F6775CC051F8} {B0C64769-72F4-42AE-B0E3-938FF8D1C145} = {AAE1B832-6032-4C5C-8154-F6775CC051F8} + {EFBE76EB-50AE-4C37-BA54-8ECBAB757EFC} = {661AB817-BF00-4B7F-88B5-9ADC84DC93A5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {78AE28B8-138A-401A-B79E-457DC5EFB188} diff --git a/misc/data/AspNetUsersData.sql b/misc/data/AspNetUsersData.sql index 51784f3..f1ad4d8 100644 --- a/misc/data/AspNetUsersData.sql +++ b/misc/data/AspNetUsersData.sql @@ -1,4 +1,4 @@ -INSERT INTO [dbo].[AspNetUsers] (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp, PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount) VALUES ('53ba34ce-96f8-452f-a714-0033136c66ee', 'admin@daarto.com', 'ADMIN@DAARTO.COM', 'admin@daarto.com', 'ADMIN@DAARTO.COM', 1, 'AQAAAAEAACcQAAAAEFKjcOnLHrAhew3z9YUjO/vGiExMDrNi54ZyUKACsW8rn1vDTleOUvJeNoEG3JU4fQ==', '76b51311-5cd0-49c2-9e19-8c82403cc117', '816087c4-46b4-4bb2-9923-6ba5dfe63832', '+30 6981908600', 1, 0, 0, 0); +INSERT INTO [dbo].[AspNetUsers] (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, PasswordHash, SecurityStamp, ConcurrencyStamp, PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount) VALUES ('53ba34ce-96f8-452f-a714-0033136c66ee', 'admin@daarto.com', 'ADMIN@DAARTO.COM', 'admin@daarto.com', 'ADMIN@DAARTO.COM', 1, 'AQAAAAEAACcQAAAAEKRm/fJQk3xhqIv7O0IyPyiPJ+W5du3Io15fIcI59hfUv1DTmDid3RhgzdJVzARpQg==', '76b51311-5cd0-49c2-9e19-8c82403cc117', '816087c4-46b4-4bb2-9923-6ba5dfe63832', '+30 6981908600', 1, 0, 0, 0); INSERT INTO [dbo].[AspNetUsers] (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, SecurityStamp, ConcurrencyStamp, PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount) VALUES ('634963b4-a2d0-4160-ad84-c088ade8dbd9', 'fprint0@liveinternet.ru', 'FPRINT0@LIVEINTERNET.RU', 'fprint0@liveinternet.ru', 'FPRINT0@LIVEINTERNET.RU', 1, '9db08d96-5f13-4a2e-af12-07106d2a0123', '992c1c2c-5e19-4911-a1ac-47819879929a', '+81 (831) 990-1658', 1, 1, 1, 0); INSERT INTO [dbo].[AspNetUsers] (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, SecurityStamp, ConcurrencyStamp, PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount) VALUES ('22ddf8d1-876f-413b-b9c8-b680ae87d23a', 'lhamfleet1@wix.com', 'LHAMFLEET1@WIX.COM', 'lhamfleet1@wix.com', 'LHAMFLEET1@WIX.COM', 0, 'ca240a34-7c09-48a3-8b6b-d0f1022db534', '7c61d4b4-a2fd-4dd2-a6bb-67de5a071394', '+263 (267) 874-0846', 1, 1, 1, 0); INSERT INTO [dbo].[AspNetUsers] (Id, UserName, NormalizedUserName, Email, NormalizedEmail, EmailConfirmed, SecurityStamp, ConcurrencyStamp, PhoneNumber, PhoneNumberConfirmed, TwoFactorEnabled, LockoutEnabled, AccessFailedCount) VALUES ('c9c36943-6e94-4f11-86a9-2f97b3dbe41e', 'grutledge2@nbcnews.com', 'GRUTLEDGE2@NBCNEWS.COM', 'grutledge2@nbcnews.com', 'GRUTLEDGE2@NBCNEWS.COM', 0, '24d1877e-23e4-46d0-bedc-306a7b4be534', 'c8ab5645-e670-411f-9b0b-6eca8f9fed2c', '+86 (977) 523-3451', 1, 0, 1, 0); diff --git a/misc/database/IdentityDb/IdentityDb.sqlproj b/misc/database/IdentityDatabase/IdentityDatabase.sqlproj similarity index 92% rename from misc/database/IdentityDb/IdentityDb.sqlproj rename to misc/database/IdentityDatabase/IdentityDatabase.sqlproj index 615da33..66712f3 100644 --- a/misc/database/IdentityDb/IdentityDb.sqlproj +++ b/misc/database/IdentityDatabase/IdentityDatabase.sqlproj @@ -3,20 +3,20 @@ Debug AnyCPU - IdentityDb + IdentityDatabase 2.0 4.1 - {f082eb24-7aa8-4eca-9856-6375b7aaaecb} + {efbe76eb-50ae-4c37-ba54-8ecbab757efc} Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider Database - IdentityDb - IdentityDb + IdentityDatabase + IdentityDatabase 1033, CI BySchemaAndSchemaType True - v4.5 + v4.7.2 CS Properties False diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetRoleClaims.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetRoleClaims.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetRoleClaims.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetRoleClaims.sql diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetRoles.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetRoles.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetRoles.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetRoles.sql diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetUserClaims.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetUserClaims.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetUserClaims.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetUserClaims.sql diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetUserLogins.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetUserLogins.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetUserLogins.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetUserLogins.sql diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetUserRoles.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetUserRoles.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetUserRoles.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetUserRoles.sql diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetUserTokens.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetUserTokens.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetUserTokens.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetUserTokens.sql diff --git a/misc/database/IdentityDb/dbo/Tables/AspNetUsers.sql b/misc/database/IdentityDatabase/dbo/Tables/AspNetUsers.sql similarity index 100% rename from misc/database/IdentityDb/dbo/Tables/AspNetUsers.sql rename to misc/database/IdentityDatabase/dbo/Tables/AspNetUsers.sql diff --git a/samples/Daarto.WebUI/Daarto.WebUI.csproj b/samples/Daarto.WebUI/Daarto.WebUI.csproj index ea183f9..8f09ebd 100644 --- a/samples/Daarto.WebUI/Daarto.WebUI.csproj +++ b/samples/Daarto.WebUI/Daarto.WebUI.csproj @@ -1,6 +1,6 @@  - netcoreapp3.1 + net6.0 aspnet-Daarto.WebUI-0BC4D06A-1285-4595-85EA-07A68DD02CF3 diff --git a/samples/Daarto.WebUI/Startup.cs b/samples/Daarto.WebUI/Startup.cs index 1272f99..76b7492 100644 --- a/samples/Daarto.WebUI/Startup.cs +++ b/samples/Daarto.WebUI/Startup.cs @@ -17,27 +17,24 @@ public Startup(IConfiguration configuration) { public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddIdentity() - .AddDapperStores(options => { - options.AddRolesTable(); - }) - .AddDefaultUI() - .AddDefaultTokenProviders(); - //services.AddDefaultIdentity() - // .AddDapperStores(); + services.AddIdentity(options => { + options.Lockout.MaxFailedAccessAttempts = 3; + }) + .AddDapperStores(options => { + options.AddRolesTable(); + }) + .AddDefaultUI() + .AddDefaultTokenProviders(); services.AddControllersWithViews(); services.AddRazorPages(); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment environment) { if (environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); diff --git a/src/AspNetCore.Identity.Dapper/AspNetCore.Identity.Dapper.csproj b/src/AspNetCore.Identity.Dapper/AspNetCore.Identity.Dapper.csproj index 53d2060..b7444ee 100644 --- a/src/AspNetCore.Identity.Dapper/AspNetCore.Identity.Dapper.csproj +++ b/src/AspNetCore.Identity.Dapper/AspNetCore.Identity.Dapper.csproj @@ -3,30 +3,40 @@ AspNetCore.Identity.Dapper AspNetCore.Identity.Dapper Georgios Manoltzas - Copyright (c) 2018 - 2019 Georgios Manoltzas + Copyright (c) 2018 - 2022 Georgios Manoltzas Dapper implementation of ASP.NET Core Identity stores. true - netstandard2.1;netcoreapp3.1 - 3.1.1 + netstandard2.1;net5.0;net6.0; + 6.0.0 $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb true true AspNetCore.Identity.DapperOrm - AspNetCore;Dapper;Identity - Fixes issue #14. - https://github.com/giorgos07/Daarto/tree/master/src/AspNetCore.Identity.Dapper MIT + https://github.com/giorgos07/Daarto/tree/master/src/AspNetCore.Identity.Dapper + Fixes issue #14. + AspNetCore;Dapper;Identity + true git https://github.com/giorgos07/Daarto/tree/master/src/AspNetCore.Identity.Dapper - true - ea4ca2df-aa2d-4174-8f13-b1e594019d86 - - - - + + + + + + + + + + + + + + + diff --git a/src/AspNetCore.Identity.Dapper/Stores/RoleStore.cs b/src/AspNetCore.Identity.Dapper/Stores/RoleStore.cs index 56d42f6..fcaed4f 100644 --- a/src/AspNetCore.Identity.Dapper/Stores/RoleStore.cs +++ b/src/AspNetCore.Identity.Dapper/Stores/RoleStore.cs @@ -13,8 +13,7 @@ namespace AspNetCore.Identity.Dapper /// The persistence store for roles. /// /// The type of the class representing a role. - public class RoleStore : RoleStore - where TRole : IdentityRole + public class RoleStore : RoleStore where TRole : IdentityRole { /// /// Constructs a new instance of . diff --git a/src/AspNetCore.Identity.Dapper/Stores/UserOnlyStore.cs b/src/AspNetCore.Identity.Dapper/Stores/UserOnlyStore.cs index eed3c2e..ef36325 100644 --- a/src/AspNetCore.Identity.Dapper/Stores/UserOnlyStore.cs +++ b/src/AspNetCore.Identity.Dapper/Stores/UserOnlyStore.cs @@ -16,9 +16,7 @@ namespace AspNetCore.Identity.Dapper /// The type representing a claim. /// The type representing a user external login. /// The type representing a user token. - public class UserOnlyStore : - UserStoreBase, - IProtectedUserStore + public class UserOnlyStore : UserStoreBase, IProtectedUserStore where TUser : IdentityUser where TKey : IEquatable where TUserClaim : IdentityUserClaim, new() diff --git a/src/AspNetCore.Identity.Dapper/Stores/UserStore.cs b/src/AspNetCore.Identity.Dapper/Stores/UserStore.cs index 7429260..96e1341 100644 --- a/src/AspNetCore.Identity.Dapper/Stores/UserStore.cs +++ b/src/AspNetCore.Identity.Dapper/Stores/UserStore.cs @@ -19,9 +19,7 @@ namespace AspNetCore.Identity.Dapper /// The type representing a user external login. /// The type representing a user token. /// The type representing a role claim. - public class UserStore : - UserStoreBase, - IProtectedUserStore + public class UserStore : UserStoreBase, IProtectedUserStore where TUser : IdentityUser where TRole : IdentityRole where TKey : IEquatable diff --git a/src/AspNetCore.Identity.Dapper/Tables/RoleClaimsTable.cs b/src/AspNetCore.Identity.Dapper/Tables/RoleClaimsTable.cs index 93e95df..5413911 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/RoleClaimsTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/RoleClaimsTable.cs @@ -12,9 +12,7 @@ namespace AspNetCore.Identity.Dapper /// /// The type of the primary key for a role. /// The type of the class representing a role claim. - public class RoleClaimsTable : - IdentityTable, - IRoleClaimsTable + public class RoleClaimsTable : IdentityTable, IRoleClaimsTable where TKey : IEquatable where TRoleClaim : IdentityRoleClaim, new() { @@ -26,9 +24,11 @@ public RoleClaimsTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnec /// public virtual async Task> GetClaimsAsync(TKey roleId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetRoleClaims] " + - "WHERE [RoleId] = @RoleId;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetRoleClaims] + WHERE [RoleId] = @RoleId; + "; var roleClaims = await DbConnection.QueryAsync(sql, new { RoleId = roleId }); return roleClaims; } diff --git a/src/AspNetCore.Identity.Dapper/Tables/RolesTable.cs b/src/AspNetCore.Identity.Dapper/Tables/RolesTable.cs index b0cca8b..fdb694b 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/RolesTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/RolesTable.cs @@ -14,9 +14,7 @@ namespace AspNetCore.Identity.Dapper /// The type of the class representing a role. /// The type of the primary key for a role. /// The type of the class representing a role claim. - public class RolesTable : - IdentityTable, - IRolesTable + public class RolesTable : IdentityTable, IRolesTable where TRole : IdentityRole where TKey : IEquatable where TRoleClaim : IdentityRoleClaim, new() @@ -29,8 +27,10 @@ public RolesTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnectionF /// public virtual async Task CreateAsync(TRole role) { - const string sql = "INSERT INTO [dbo].[AspNetRoles] " + - "VALUES (@Id, @Name, @NormalizedName, @ConcurrencyStamp);"; + const string sql = @" + INSERT INTO [dbo].[AspNetRoles] + VALUES (@Id, @Name, @NormalizedName, @ConcurrencyStamp); + "; var rowsInserted = await DbConnection.ExecuteAsync(sql, new { role.Id, role.Name, @@ -42,36 +42,44 @@ public virtual async Task CreateAsync(TRole role) { /// public virtual async Task DeleteAsync(TKey roleId) { - const string sql = "DELETE " + - "FROM [dbo].[AspNetRoles] " + - "WHERE [Id] = @Id;"; + const string sql = @" + DELETE + FROM [dbo].[AspNetRoles] + WHERE [Id] = @Id; + "; var rowsDeleted = await DbConnection.ExecuteAsync(sql, new { Id = roleId }); return rowsDeleted == 1; } /// public virtual async Task FindByIdAsync(TKey roleId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetRoles] " + - "WHERE [Id] = @Id;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetRoles] + WHERE [Id] = @Id; + "; var role = await DbConnection.QuerySingleOrDefaultAsync(sql, new { Id = roleId }); return role; } /// public virtual async Task FindByNameAsync(string normalizedName) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetRoles] " + - "WHERE [NormalizedName] = @NormalizedName;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetRoles] + WHERE [NormalizedName] = @NormalizedName; + "; var role = await DbConnection.QuerySingleOrDefaultAsync(sql, new { NormalizedName = normalizedName }); return role; } /// public virtual async Task UpdateAsync(TRole role, IList claims = null) { - const string updateRoleSql = "UPDATE [dbo].[AspNetRoles] " + - "SET [Name] = @Name, [NormalizedName] = @NormalizedName, [ConcurrencyStamp] = @ConcurrencyStamp " + - "WHERE [Id] = @Id;"; + const string updateRoleSql = @" + UPDATE [dbo].[AspNetRoles] + SET [Name] = @Name, [NormalizedName] = @NormalizedName, [ConcurrencyStamp] = @ConcurrencyStamp + WHERE [Id] = @Id; + "; using (var transaction = DbConnection.BeginTransaction()) { await DbConnection.ExecuteAsync(updateRoleSql, new { role.Name, @@ -80,14 +88,18 @@ public virtual async Task UpdateAsync(TRole role, IList claims role.Id }, transaction); if (claims?.Count() > 0) { - const string deleteClaimsSql = "DELETE " + - "FROM [dbo].[AspNetRoleClaims] " + - "WHERE [RoleId] = @RoleId;"; + const string deleteClaimsSql = @" + DELETE + FROM [dbo].[AspNetRoleClaims] + WHERE [RoleId] = @RoleId; + "; await DbConnection.ExecuteAsync(deleteClaimsSql, new { RoleId = role.Id }, transaction); - const string insertClaimsSql = "INSERT INTO [dbo].[AspNetRoleClaims] (RoleId, ClaimType, ClaimValue) " + - "VALUES (@RoleId, @ClaimType, @ClaimValue);"; + const string insertClaimsSql = @" + INSERT INTO [dbo].[AspNetRoleClaims] (RoleId, ClaimType, ClaimValue) + VALUES (@RoleId, @ClaimType, @ClaimValue); + "; await DbConnection.ExecuteAsync(insertClaimsSql, claims.Select(x => new { RoleId = role.Id, x.ClaimType, diff --git a/src/AspNetCore.Identity.Dapper/Tables/UserClaimsTable.cs b/src/AspNetCore.Identity.Dapper/Tables/UserClaimsTable.cs index 46b0b6b..d0105ba 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/UserClaimsTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/UserClaimsTable.cs @@ -12,9 +12,7 @@ namespace AspNetCore.Identity.Dapper /// /// The type of the primary key for a user. /// The type representing a claim. - public class UserClaimsTable : - IdentityTable, - IUserClaimsTable + public class UserClaimsTable : IdentityTable, IUserClaimsTable where TKey : IEquatable where TUserClaim : IdentityUserClaim, new() { @@ -26,9 +24,11 @@ public UserClaimsTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnec /// public virtual async Task> GetClaimsAsync(TKey userId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserClaims] " + - "WHERE [UserId] = @UserId;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserClaims] + WHERE [UserId] = @UserId; + "; var userClaims = await DbConnection.QueryAsync(sql, new { UserId = userId }); return userClaims; } diff --git a/src/AspNetCore.Identity.Dapper/Tables/UserLoginsTable.cs b/src/AspNetCore.Identity.Dapper/Tables/UserLoginsTable.cs index 10f76fa..db9f242 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/UserLoginsTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/UserLoginsTable.cs @@ -13,9 +13,7 @@ namespace AspNetCore.Identity.Dapper /// The type representing a user. /// The type of the primary key for a user. /// The type representing a user external login. - public class UserLoginsTable : - IdentityTable, - IUserLoginsTable + public class UserLoginsTable : IdentityTable, IUserLoginsTable where TUser : IdentityUser where TKey : IEquatable where TUserLogin : IdentityUserLogin, new() @@ -28,19 +26,23 @@ public UserLoginsTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnec /// public virtual async Task> GetLoginsAsync(TKey userId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserLogins] " + - "WHERE [UserId] = @UserId;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserLogins] + WHERE [UserId] = @UserId; + "; var userLogins = await DbConnection.QueryAsync(sql, new { UserId = userId }); return userLogins; } /// public virtual async Task FindByLoginAsync(string loginProvider, string providerKey) { - const string sql = "SELECT [u].* " + - "FROM [dbo].[AspNetUsers] AS [u] " + - "INNER JOIN [dbo].[AspNetUserLogins] AS [ul] ON [ul].[UserId] = [u].[Id] " + - "WHERE [ul].[LoginProvider] = @LoginProvider AND [ul].[ProviderKey] = @ProviderKey;"; + const string sql = @" + SELECT [u].* + FROM [dbo].[AspNetUsers] AS [u] + INNER JOIN [dbo].[AspNetUserLogins] AS [ul] ON [ul].[UserId] = [u].[Id] + WHERE [ul].[LoginProvider] = @LoginProvider AND [ul].[ProviderKey] = @ProviderKey; + "; var user = await DbConnection.QuerySingleOrDefaultAsync(sql, new { LoginProvider = loginProvider, ProviderKey = providerKey @@ -50,9 +52,11 @@ public virtual async Task FindByLoginAsync(string loginProvider, string p /// public virtual async Task FindUserLoginAsync(string loginProvider, string providerKey) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserLogins] " + - "WHERE [LoginProvider] = @LoginProvider AND [ProviderKey] = @ProviderKey;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserLogins] + WHERE [LoginProvider] = @LoginProvider AND [ProviderKey] = @ProviderKey; + "; var userLogin = await DbConnection.QuerySingleOrDefaultAsync(sql, new { LoginProvider = loginProvider, ProviderKey = providerKey @@ -62,9 +66,11 @@ public virtual async Task FindUserLoginAsync(string loginProvider, s /// public virtual async Task FindUserLoginAsync(TKey userId, string loginProvider, string providerKey) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserLogins] " + - "WHERE [UserId] = @UserId AND [LoginProvider] = @LoginProvider AND [ProviderKey] = @ProviderKey;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserLogins] + WHERE [UserId] = @UserId AND [LoginProvider] = @LoginProvider AND [ProviderKey] = @ProviderKey; + "; var userLogin = await DbConnection.QuerySingleOrDefaultAsync(sql, new { UserId = userId, LoginProvider = loginProvider, diff --git a/src/AspNetCore.Identity.Dapper/Tables/UserRolesTable.cs b/src/AspNetCore.Identity.Dapper/Tables/UserRolesTable.cs index 756390a..664bba2 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/UserRolesTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/UserRolesTable.cs @@ -13,9 +13,7 @@ namespace AspNetCore.Identity.Dapper /// The type representing a role. /// The type of the primary key for a user. /// The type representing a user role. - public class UserRolesTable : - IdentityTable, - IUserRolesTable + public class UserRolesTable : IdentityTable, IUserRolesTable where TRole : IdentityRole where TKey : IEquatable where TUserRole : IdentityUserRole, new() @@ -28,19 +26,23 @@ public UserRolesTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnect /// public virtual async Task> GetRolesAsync(TKey userId) { - const string sql = "SELECT [r].* " + - "FROM [dbo].[AspNetRoles] AS [r] " + - "INNER JOIN [dbo].[AspNetUserRoles] AS [ur] ON [ur].[RoleId] = [r].[Id] " + - "WHERE [ur].[UserId] = @UserId;"; + const string sql = @" + SELECT [r].* + FROM [dbo].[AspNetRoles] AS [r] + INNER JOIN [dbo].[AspNetUserRoles] AS [ur] ON [ur].[RoleId] = [r].[Id] + WHERE [ur].[UserId] = @UserId; + "; var userRoles = await DbConnection.QueryAsync(sql, new { UserId = userId }); return userRoles; } /// public virtual async Task FindUserRoleAsync(TKey userId, TKey roleId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserRoles] " + - "WHERE [UserId] = @UserId AND [RoleId] = @RoleId;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserRoles] + WHERE [UserId] = @UserId AND [RoleId] = @RoleId; + "; var userRole = await DbConnection.QuerySingleOrDefaultAsync(sql, new { UserId = userId, RoleId = roleId diff --git a/src/AspNetCore.Identity.Dapper/Tables/UserTokensTable.cs b/src/AspNetCore.Identity.Dapper/Tables/UserTokensTable.cs index 8df8616..59dc1f1 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/UserTokensTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/UserTokensTable.cs @@ -12,9 +12,7 @@ namespace AspNetCore.Identity.Dapper /// /// The type of the primary key for a user. /// The type representing a user token. - public class UserTokensTable : - IdentityTable, - IUserTokensTable + public class UserTokensTable : IdentityTable, IUserTokensTable where TKey : IEquatable where TUserToken : IdentityUserToken, new() { @@ -26,18 +24,22 @@ public UserTokensTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnec /// public virtual async Task> GetTokensAsync(TKey userId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserTokens] " + - "WHERE [UserId] = @UserId;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserTokens] + WHERE [UserId] = @UserId; + "; var userTokens = await DbConnection.QueryAsync(sql, new { UserId = userId }); return userTokens; } /// public virtual async Task FindTokenAsync(TKey userId, string loginProvider, string name) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUserTokens] " + - "WHERE [UserId] = @UserId AND [LoginProvider] = @LoginProvider AND [Name] = @Name;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUserTokens] + WHERE [UserId] = @UserId AND [LoginProvider] = @LoginProvider AND [Name] = @Name; + "; var token = await DbConnection.QuerySingleOrDefaultAsync(sql, new { UserId = userId, LoginProvider = loginProvider, diff --git a/src/AspNetCore.Identity.Dapper/Tables/UsersTable.cs b/src/AspNetCore.Identity.Dapper/Tables/UsersTable.cs index 690af4b..4f21c30 100644 --- a/src/AspNetCore.Identity.Dapper/Tables/UsersTable.cs +++ b/src/AspNetCore.Identity.Dapper/Tables/UsersTable.cs @@ -18,9 +18,7 @@ namespace AspNetCore.Identity.Dapper /// The type representing a user role. /// The type representing a user external login. /// The type representing a user token. - public class UsersTable : - IdentityTable, - IUsersTable + public class UsersTable : IdentityTable, IUsersTable where TUser : IdentityUser where TKey : IEquatable where TUserClaim : IdentityUserClaim, new() @@ -36,9 +34,10 @@ public UsersTable(IDbConnectionFactory dbConnectionFactory) : base(dbConnectionF /// public virtual async Task CreateAsync(TUser user) { - const string sql = "INSERT INTO [dbo].[AspNetUsers] " + - "VALUES (@Id, @UserName, @NormalizedUserName, @Email, @NormalizedEmail, @EmailConfirmed, @PasswordHash, @SecurityStamp, @ConcurrencyStamp, " + - "@PhoneNumber, @PhoneNumberConfirmed, @TwoFactorEnabled, @LockoutEnd, @LockoutEnabled, @AccessFailedCount);"; + const string sql = @" + INSERT INTO [dbo].[AspNetUsers] + VALUES (@Id, @UserName, @NormalizedUserName, @Email, @NormalizedEmail, @EmailConfirmed, @PasswordHash, @SecurityStamp, @ConcurrencyStamp, @PhoneNumber, @PhoneNumberConfirmed, @TwoFactorEnabled, @LockoutEnd, @LockoutEnabled, @AccessFailedCount); + "; var rowsInserted = await DbConnection.ExecuteAsync(sql, new { user.Id, user.UserName, @@ -61,54 +60,71 @@ public virtual async Task CreateAsync(TUser user) { /// public virtual async Task DeleteAsync(TKey userId) { - const string sql = "DELETE " + - "FROM [dbo].[AspNetUsers] " + - "WHERE [Id] = @Id;"; + const string sql = @" + DELETE + FROM [dbo].[AspNetUsers] + WHERE [Id] = @Id; + "; var rowsDeleted = await DbConnection.ExecuteAsync(sql, new { Id = userId }); return rowsDeleted == 1; } /// public virtual async Task FindByIdAsync(TKey userId) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUsers] " + - "WHERE [Id] = @Id;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUsers] + WHERE [Id] = @Id; + "; var user = await DbConnection.QuerySingleOrDefaultAsync(sql, new { Id = userId }); return user; } /// public virtual async Task FindByNameAsync(string normalizedUserName) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUsers] " + - "WHERE [NormalizedUserName] = @NormalizedUserName;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUsers] + WHERE [NormalizedUserName] = @NormalizedUserName; + "; var user = await DbConnection.QuerySingleOrDefaultAsync(sql, new { NormalizedUserName = normalizedUserName }); return user; } /// public virtual async Task FindByEmailAsync(string normalizedEmail) { - const string command = "SELECT * " + - "FROM [dbo].[AspNetUsers] " + - "WHERE [NormalizedEmail] = @NormalizedEmail;"; - var user = await DbConnection.QuerySingleOrDefaultAsync(command, new { NormalizedEmail = normalizedEmail }); + const string sql = @" + SELECT * + FROM [dbo].[AspNetUsers] + WHERE [NormalizedEmail] = @NormalizedEmail; + "; + var user = await DbConnection.QuerySingleOrDefaultAsync(sql, new { NormalizedEmail = normalizedEmail }); return user; } /// - public virtual Task UpdateAsync(TUser user, IList claims, IList logins, IList tokens) { - return UpdateAsync(user, claims, null, logins, tokens); - } + public virtual Task UpdateAsync(TUser user, IList claims, IList logins, IList tokens) => UpdateAsync(user, claims, null, logins, tokens); /// public virtual async Task UpdateAsync(TUser user, IList claims, IList roles, IList logins, IList tokens) { - const string updateUserSql = - "UPDATE [dbo].[AspNetUsers] " + - "SET [UserName] = @UserName, [NormalizedUserName] = @NormalizedUserName, [Email] = @Email, [NormalizedEmail] = @NormalizedEmail, [EmailConfirmed] = @EmailConfirmed, " + - "[PasswordHash] = @PasswordHash, [SecurityStamp] = @SecurityStamp, [ConcurrencyStamp] = @ConcurrencyStamp, [PhoneNumber] = @PhoneNumber, " + - "[PhoneNumberConfirmed] = @PhoneNumberConfirmed, [TwoFactorEnabled] = @TwoFactorEnabled, [LockoutEnd] = @LockoutEnd, [LockoutEnabled] = @LockoutEnabled, " + - "[AccessFailedCount] = @AccessFailedCount " + - "WHERE [Id] = @Id;"; + const string updateUserSql = @" + UPDATE [dbo].[AspNetUsers] + SET [UserName] = @UserName, + [NormalizedUserName] = @NormalizedUserName, + [Email] = @Email, + [NormalizedEmail] = @NormalizedEmail, + [EmailConfirmed] = @EmailConfirmed, + [PasswordHash] = @PasswordHash, + [SecurityStamp] = @SecurityStamp, + [ConcurrencyStamp] = @ConcurrencyStamp, + [PhoneNumber] = @PhoneNumber, + [PhoneNumberConfirmed] = @PhoneNumberConfirmed, + [TwoFactorEnabled] = @TwoFactorEnabled, + [LockoutEnd] = @LockoutEnd, + [LockoutEnabled] = @LockoutEnabled, + [AccessFailedCount] = @AccessFailedCount + WHERE [Id] = @Id; + "; using (var transaction = DbConnection.BeginTransaction()) { await DbConnection.ExecuteAsync(updateUserSql, new { user.UserName, @@ -128,12 +144,16 @@ public virtual async Task UpdateAsync(TUser user, IList claims user.Id }, transaction); if (claims?.Count() > 0) { - const string deleteClaimsSql = "DELETE " + - "FROM [dbo].[AspNetUserClaims] " + - "WHERE [UserId] = @UserId;"; + const string deleteClaimsSql = @" + DELETE + FROM [dbo].[AspNetUserClaims] + WHERE [UserId] = @UserId; + "; await DbConnection.ExecuteAsync(deleteClaimsSql, new { UserId = user.Id }, transaction); - const string insertClaimsSql = "INSERT INTO [dbo].[AspNetUserClaims] (UserId, ClaimType, ClaimValue) " + - "VALUES (@UserId, @ClaimType, @ClaimValue);"; + const string insertClaimsSql = @" + INSERT INTO [dbo].[AspNetUserClaims] (UserId, ClaimType, ClaimValue) + VALUES (@UserId, @ClaimType, @ClaimValue); + "; await DbConnection.ExecuteAsync(insertClaimsSql, claims.Select(x => new { UserId = user.Id, x.ClaimType, @@ -141,24 +161,32 @@ public virtual async Task UpdateAsync(TUser user, IList claims }), transaction); } if (roles?.Count() > 0) { - const string deleteRolesSql = "DELETE " + - "FROM [dbo].[AspNetUserRoles] " + - "WHERE [UserId] = @UserId;"; + const string deleteRolesSql = @" + DELETE + FROM [dbo].[AspNetUserRoles] + WHERE [UserId] = @UserId; + "; await DbConnection.ExecuteAsync(deleteRolesSql, new { UserId = user.Id }, transaction); - const string insertRolesSql = "INSERT INTO [dbo].[AspNetUserRoles] (UserId, RoleId) " + - "VALUES (@UserId, @RoleId);"; + const string insertRolesSql = @" + INSERT INTO [dbo].[AspNetUserRoles] (UserId, RoleId) + VALUES (@UserId, @RoleId); + "; await DbConnection.ExecuteAsync(insertRolesSql, roles.Select(x => new { UserId = user.Id, x.RoleId }), transaction); } if (logins?.Count() > 0) { - const string deleteLoginsSql = "DELETE " + - "FROM [dbo].[AspNetUserLogins] " + - "WHERE [UserId] = @UserId;"; + const string deleteLoginsSql = @" + DELETE + FROM [dbo].[AspNetUserLogins] + WHERE [UserId] = @UserId; + "; await DbConnection.ExecuteAsync(deleteLoginsSql, new { UserId = user.Id }, transaction); - const string insertLoginsSql = "INSERT INTO [dbo].[AspNetUserLogins] (LoginProvider, ProviderKey, ProviderDisplayName, UserId) " + - "VALUES (@LoginProvider, @ProviderKey, @ProviderDisplayName, @UserId);"; + const string insertLoginsSql = @" + INSERT INTO [dbo].[AspNetUserLogins] (LoginProvider, ProviderKey, ProviderDisplayName, UserId) + VALUES (@LoginProvider, @ProviderKey, @ProviderDisplayName, @UserId); + "; await DbConnection.ExecuteAsync(insertLoginsSql, logins.Select(x => new { x.LoginProvider, x.ProviderKey, @@ -167,12 +195,16 @@ public virtual async Task UpdateAsync(TUser user, IList claims }), transaction); } if (tokens?.Count() > 0) { - const string deleteTokensSql = "DELETE " + - "FROM [dbo].[AspNetUserTokens] " + - "WHERE [UserId] = @UserId;"; + const string deleteTokensSql = @" + DELETE + FROM [dbo].[AspNetUserTokens] + WHERE [UserId] = @UserId; + "; await DbConnection.ExecuteAsync(deleteTokensSql, new { UserId = user.Id }, transaction); - const string insertTokensSql = "INSERT INTO [dbo].[AspNetUserTokens] (UserId, LoginProvider, Name, Value) " + - "VALUES (@UserId, @LoginProvider, @Name, @Value);"; + const string insertTokensSql = @" + INSERT INTO [dbo].[AspNetUserTokens] (UserId, LoginProvider, Name, Value) + VALUES (@UserId, @LoginProvider, @Name, @Value); + "; await DbConnection.ExecuteAsync(insertTokensSql, tokens.Select(x => new { x.UserId, x.LoginProvider, @@ -192,21 +224,25 @@ public virtual async Task UpdateAsync(TUser user, IList claims /// public virtual async Task> GetUsersInRoleAsync(string roleName) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUsers] AS [u] " + - "INNER JOIN [dbo].[AspNetUserRoles] AS [ur] ON [u].[Id] = [ur].[UserId] " + - "INNER JOIN [dbo].[AspNetRoles] AS [r] ON [ur].[RoleId] = [r].[Id] " + - "WHERE [r].[Name] = @RoleName;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUsers] AS [u] + INNER JOIN [dbo].[AspNetUserRoles] AS [ur] ON [u].[Id] = [ur].[UserId] + INNER JOIN [dbo].[AspNetRoles] AS [r] ON [ur].[RoleId] = [r].[Id] + WHERE [r].[Name] = @RoleName; + "; var users = await DbConnection.QueryAsync(sql, new { RoleName = roleName }); return users; } /// public virtual async Task> GetUsersForClaimAsync(Claim claim) { - const string sql = "SELECT * " + - "FROM [dbo].[AspNetUsers] AS [u] " + - "INNER JOIN [dbo].[AspNetUserClaims] AS [uc] ON [u].[Id] = [uc].[UserId] " + - "WHERE [uc].[ClaimType] = @ClaimType AND [uc].[ClaimValue] = @ClaimValue;"; + const string sql = @" + SELECT * + FROM [dbo].[AspNetUsers] AS [u] + INNER JOIN [dbo].[AspNetUserClaims] AS [uc] ON [u].[Id] = [uc].[UserId] + WHERE [uc].[ClaimType] = @ClaimType AND [uc].[ClaimValue] = @ClaimValue; + "; var users = await DbConnection.QueryAsync(sql, new { ClaimType = claim.Type, ClaimValue = claim.Value