This is the continuation from my previous two post. Have a look at those below.
Today we will move our users from IdentityServer’s in-memory to SQL Server.
So first of all we need to install below nuget packages. Please note this is applicable for ASP.NET Core 1.1 version only.
1.Install IdentityServer4.EntityFramework nuget 1.x version. Use the latest one.
2.Add Microsoft.EntityFrameworkCore.SqlServer. Use the latest 1.x version.
3.Add Microsoft.EntityFrameworkCore.Design. Use the latest 1.x version.
4.Now edit the project and add below tag.
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.5" /> </ItemGroup>
5.Now got to your solution directory and check whether Entity Framework is correctly installed. Use the “dotnet ef” command.
6.Create a database named “IdentityServices” in SQL Server.
7.Update your Config.cs as below.
using IdentityServer4.Models; using IdentityServer4.Test; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace IdentityServices { public class Config { public static IEnumerable<ApiResource> GetApiResources() { return new List<ApiResource> { new ApiResource("api1", "My API") }; } public static IEnumerable<Client> GetClients() { return new List<Client> { new Client { ClientId = "client", AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, ClientSecrets = { new Secret("secret".Sha256()) }, AllowedScopes = { "api1" } } }; } } }
8.Add a class named User.
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; namespace IdentityServices { public class User { [Key] public string Id { get; set; } public string Email { get; set; } public bool Active { get; set; } public string Password { get; set; } } }
9.Now add a DbContext class name “ApplicationDbContext”.
using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace IdentityServices { public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<User> Users { get; set; } } }
10.Now add a class named DataAcess.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using IdentityServer4.Models; using IdentityServer4.Validation; using System.Security.Claims; using IdentityModel; using IdentityServer4.Extensions; using IdentityServer4.Services; namespace IdentityServices { public interface IAuthRepository { User GetUserById(string id); User GetUserByUsername(string username); bool ValidatePassword(string username, string plainTextPassword); } public class AuthRepository : IAuthRepository { private ApplicationDbContext db; public AuthRepository(ApplicationDbContext context) { db = context; } public User GetUserById(string id) { var user = db.Users.Where(u => u.Id == id).FirstOrDefault(); return user; } public User GetUserByUsername(string username) { var user = db.Users.Where(u => String.Equals(u.Email, username)).FirstOrDefault(); return user; } public bool ValidatePassword(string username, string plainTextPassword) { var user = db.Users.Where(u => String.Equals(u.Email, username)).FirstOrDefault(); if (user == null) return false; if (String.Equals(plainTextPassword, user.Password)) return true; return false; } } public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator { IAuthRepository _rep; public ResourceOwnerPasswordValidator(IAuthRepository rep) { this._rep = rep; } public Task ValidateAsync(ResourceOwnerPasswordValidationContext context) { if (_rep.ValidatePassword(context.UserName, context.Password)) { context.Result = new GrantValidationResult(_rep.GetUserByUsername(context.UserName).Id, "password", null, "local", null); return Task.FromResult(context.Result); } context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "The username and password do not match", null); return Task.FromResult(context.Result); } } public class ProfileService : IProfileService { private IAuthRepository _repository; public ProfileService(IAuthRepository rep) { this._repository = rep; } public Task GetProfileDataAsync(ProfileDataRequestContext context) { try { var subjectId = context.Subject.GetSubjectId(); var user = _repository.GetUserById(subjectId); var claims = new List<Claim> { new Claim(JwtClaimTypes.Subject, user.Id.ToString()), //add as many claims as you want!new Claim(JwtClaimTypes.Email, user.Email),new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean) }; context.IssuedClaims = claims; return Task.FromResult(0); } catch (Exception x) { return Task.FromResult(0); } } public Task IsActiveAsync(IsActiveContext context) { var user = _repository.GetUserById(context.Subject.GetSubjectId()); context.IsActive = (user != null) && user.Active; return Task.FromResult(0); } } }
11.Now update your Startup.cs.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System.Reflection; using Microsoft.EntityFrameworkCore; using IdentityServer4.EntityFramework.Mappers; using IdentityServer4.EntityFramework.DbContexts; using IdentityServer4.Validation; using IdentityServer4.Services; namespace IdentityServices { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 const string connectionString = @"Data Source=.;database=IdentityServices;uid=sa;pwd=password;"; string migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; public void ConfigureServices(IServiceCollection services) { services.AddTransient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>() .AddTransient<IProfileService, ProfileService>() .AddTransient<IAuthRepository, AuthRepository>(); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString) ); services.AddIdentityServer() .AddDeveloperSigningCredential() .AddInMemoryApiResources(Config.GetApiResources()) .AddInMemoryClients(Config.GetClients()) .AddConfigurationStore(builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly))) .AddOperationalStore(builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly))); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { InitializeDatabase(app); loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseIdentityServer(); } private void InitializeDatabase(IApplicationBuilder app) { using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) { serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate(); serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate(); var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>(); context.Database.Migrate(); if (!context.Clients.Any()) { foreach (var client in Config.GetClients()) { context.Clients.Add(client.ToEntity()); } context.SaveChanges(); } //if (!context.IdentityResources.Any()) //{ // foreach (var resource in Config.GetIdentityResources()) // { // context.IdentityResources.Add(resource.ToEntity()); // } // context.SaveChanges(); //} if (!context.ApiResources.Any()) { foreach (var resource in Config.GetApiResources()) { context.ApiResources.Add(resource.ToEntity()); } context.SaveChanges(); } } } } }
12.Now run these migrations, one by one.
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb dotnet ef migrations add InitialIdentityServerApplicationDbMigration -c ApplicationDbContext -o Data/Migrations/IdentityServer/ApplicationDb
13.Now run the IdentityService application and check your database.
14.Add a user to your table User.
15.Now check with Postman.
You must be logged in to post a comment.