From aa71156974a0a58c28386b4473d0725aabad29d6 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 20 Dec 2020 00:18:53 +0200 Subject: [PATCH] Data access --- IdentityServer.Api/appsettings.json | 4 +++ IdentityServer.Domain.Data/Class1.cs | 8 ------ .../DbContexts/IdentityDbContext.cs | 25 ++++++++++++++++++ .../DependencyInjectionExtensions.cs | 26 +++++++++++++++++++ .../AppUserConfiguration.cs | 15 +++++++++++ .../IdentityServer.Domain.Data.csproj | 8 ++++++ .../Repositories/IdentityRepository.cs | 23 ++++++++++++++++ .../Scripts/01.AppUser table.sql | 18 +++++++++++++ IdentityServer.Domain/Class1.cs | 8 ------ IdentityServer.Domain/Entities/AppUser.cs | 12 +++++++++ .../Repositories/IIdentityRepository.cs | 10 +++++++ dependencies.props | 1 + 12 files changed, 142 insertions(+), 16 deletions(-) delete mode 100644 IdentityServer.Domain.Data/Class1.cs create mode 100644 IdentityServer.Domain.Data/DbContexts/IdentityDbContext.cs create mode 100644 IdentityServer.Domain.Data/DependencyInjectionExtensions.cs create mode 100644 IdentityServer.Domain.Data/EntityTypeConfiguration/AppUserConfiguration.cs create mode 100644 IdentityServer.Domain.Data/Repositories/IdentityRepository.cs create mode 100644 IdentityServer.Domain.Data/Scripts/01.AppUser table.sql delete mode 100644 IdentityServer.Domain/Class1.cs create mode 100644 IdentityServer.Domain/Entities/AppUser.cs create mode 100644 IdentityServer.Domain/Repositories/IIdentityRepository.cs diff --git a/IdentityServer.Api/appsettings.json b/IdentityServer.Api/appsettings.json index d9d9a9b..2e73b4d 100644 --- a/IdentityServer.Api/appsettings.json +++ b/IdentityServer.Api/appsettings.json @@ -1,4 +1,8 @@ { + "urls": "http://*:5062", + "ConnectionStrings": { + "DatabaseConnection": "Server=***REMOVED***;Database=IdentityServer_dev;User Id=sa;Password=***REMOVED***;MultipleActiveResultSets=true" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/IdentityServer.Domain.Data/Class1.cs b/IdentityServer.Domain.Data/Class1.cs deleted file mode 100644 index 17bf3f4..0000000 --- a/IdentityServer.Domain.Data/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace IdentityServer.Domain.Data -{ - public class Class1 - { - } -} diff --git a/IdentityServer.Domain.Data/DbContexts/IdentityDbContext.cs b/IdentityServer.Domain.Data/DbContexts/IdentityDbContext.cs new file mode 100644 index 0000000..87919c3 --- /dev/null +++ b/IdentityServer.Domain.Data/DbContexts/IdentityDbContext.cs @@ -0,0 +1,25 @@ +using IdentityServer.Domain.Data.EntityTypeConfiguration; +using IdentityServer.Domain.Entities; +using Microsoft.EntityFrameworkCore; + +namespace IdentityServer.Domain.Data.DbContexts +{ + public class IdentityDbContext : DbContext + { + public DbSet AppUsers { get; set; } + + public IdentityDbContext(DbContextOptions options) + : base(options) + { + base.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll; + base.ChangeTracker.AutoDetectChangesEnabled = true; + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.ApplyConfiguration(new AppUserConfiguration()); + } + } +} diff --git a/IdentityServer.Domain.Data/DependencyInjectionExtensions.cs b/IdentityServer.Domain.Data/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..959cdc6 --- /dev/null +++ b/IdentityServer.Domain.Data/DependencyInjectionExtensions.cs @@ -0,0 +1,26 @@ +using IdentityServer.Domain.Data.DbContexts; +using IdentityServer.Domain.Data.Repositories; +using IdentityServer.Domain.Repositories; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.EntityFrameworkCore; + +namespace IdentityServer.Domain.Data +{ + public static class DependencyInjectionExtensions + { + public static void AddDataAccess(this IServiceCollection services) + { + services.AddScoped(); + + services + .AddDbContextPool( + (serviceProvider, options) => + { + var configuration = serviceProvider.GetService(); + var connectionString = configuration.GetConnectionString("DatabaseConnection"); + options.UseSqlServer(connectionString); + }); + } + } +} diff --git a/IdentityServer.Domain.Data/EntityTypeConfiguration/AppUserConfiguration.cs b/IdentityServer.Domain.Data/EntityTypeConfiguration/AppUserConfiguration.cs new file mode 100644 index 0000000..46add0c --- /dev/null +++ b/IdentityServer.Domain.Data/EntityTypeConfiguration/AppUserConfiguration.cs @@ -0,0 +1,15 @@ +using IdentityServer.Domain.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace IdentityServer.Domain.Data.EntityTypeConfiguration +{ + class AppUserConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("AppUser").HasKey(key => key.UserId); + builder.Property(z => z.UserId).ValueGeneratedOnAdd(); + } + } +} diff --git a/IdentityServer.Domain.Data/IdentityServer.Domain.Data.csproj b/IdentityServer.Domain.Data/IdentityServer.Domain.Data.csproj index 9f5c4f4..9f87f66 100644 --- a/IdentityServer.Domain.Data/IdentityServer.Domain.Data.csproj +++ b/IdentityServer.Domain.Data/IdentityServer.Domain.Data.csproj @@ -4,4 +4,12 @@ netstandard2.0 + + + + + + + + diff --git a/IdentityServer.Domain.Data/Repositories/IdentityRepository.cs b/IdentityServer.Domain.Data/Repositories/IdentityRepository.cs new file mode 100644 index 0000000..f847f3c --- /dev/null +++ b/IdentityServer.Domain.Data/Repositories/IdentityRepository.cs @@ -0,0 +1,23 @@ +using IdentityServer.Domain.Data.DbContexts; +using IdentityServer.Domain.Entities; +using IdentityServer.Domain.Repositories; +using Microsoft.EntityFrameworkCore; +using System.Threading.Tasks; + +namespace IdentityServer.Domain.Data.Repositories +{ + class IdentityRepository : IIdentityRepository + { + private readonly IdentityDbContext _dbContext; + + public IdentityRepository(IdentityDbContext dbContext) + { + _dbContext = dbContext; + } + + public Task GetAppUser(string userName, string password) + { + return _dbContext.AppUsers.FirstOrDefaultAsync(z => z.UserName == userName && z.Password == password); + } + } +} diff --git a/IdentityServer.Domain.Data/Scripts/01.AppUser table.sql b/IdentityServer.Domain.Data/Scripts/01.AppUser table.sql new file mode 100644 index 0000000..46cba40 --- /dev/null +++ b/IdentityServer.Domain.Data/Scripts/01.AppUser table.sql @@ -0,0 +1,18 @@ +if not exists (select top 1 1 from sys.objects where name = 'AppUser' and type = 'U') +begin + create table AppUser + ( + UserId int identity(0, 1) constraint PK_AppUser primary key, + UserName varchar(100) not null, + [Password] varchar(100) not null, + CreationDate datetime constraint DF_AppUser_CreationDate default getdate() + ) +end +go + +if not exists (select top 1 1 from AppUser) +begin + insert into AppUser(UserName, [Password]) + select '***REMOVED***', '***REMOVED***' +end +go \ No newline at end of file diff --git a/IdentityServer.Domain/Class1.cs b/IdentityServer.Domain/Class1.cs deleted file mode 100644 index a043e86..0000000 --- a/IdentityServer.Domain/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace IdentityServer.Domain -{ - public class Class1 - { - } -} diff --git a/IdentityServer.Domain/Entities/AppUser.cs b/IdentityServer.Domain/Entities/AppUser.cs new file mode 100644 index 0000000..b9ed4e4 --- /dev/null +++ b/IdentityServer.Domain/Entities/AppUser.cs @@ -0,0 +1,12 @@ +using System; + +namespace IdentityServer.Domain.Entities +{ + public class AppUser + { + public int UserId { get; set; } + public string UserName { get; set; } + public string Password { get; set; } + public DateTime CreationDate { get; set; } + } +} diff --git a/IdentityServer.Domain/Repositories/IIdentityRepository.cs b/IdentityServer.Domain/Repositories/IIdentityRepository.cs new file mode 100644 index 0000000..1c6f544 --- /dev/null +++ b/IdentityServer.Domain/Repositories/IIdentityRepository.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace IdentityServer.Domain.Repositories +{ + public interface IIdentityRepository + { + } +} diff --git a/dependencies.props b/dependencies.props index 40ace42..b606756 100644 --- a/dependencies.props +++ b/dependencies.props @@ -9,5 +9,6 @@ 7.0.0 6.0.0 5.3.1 + 3.1.3 \ No newline at end of file