IdentityServer4 + SignalR Core +RABBITMQ build Web Instant Messaging (ii)
IDENTITYSERVER4 User Center seed data
Above has created all the database up and down migration code, here began the migration of seed data, EF Core 2.1 Just added the function of seed data, document address, the first idea is to use this way, looks very concise and convenient, Need to be configured in Onmodelcreating, but the 2 database contexts in IdentityServer4 I don't know how to configure it, so I'll use it in a more primitive way.
1. Add the client Apiresource identityrecouse seed data in Seeddata;
Public StaticList<client>clients () {return NewList<client> { Newclient{//Client IDClientId ="chat_client", //Client NameClientName ="Chat Client", //Token effective durationAccesstokenlifetime =3600, //Configure token type, reference as reference type, data does not exist in tokenAccesstokentype=ACCESSTOKENTYPE.JWT,//Configuring the client licensing modeallowedgranttypes=Granttypes.resourceownerpassword,//Configure client Connection passwordsclientsecrets={NewSecret ("123123". SHA256 ())},//scope of requests allowed by the clientallowedscopes={ "Chatapi", IdentityServerConstants.StandardScopes.OfflineAccess, Identityserverco Nstants. Standardscopes.openid, IdentityServerConstants.StandardScopes.Profile},//allow offline, that is, open Refresh_tokenAllowofflineaccess =true, Requireclientsecret=false } }; } Public StaticIenumerable<apiresource>apiresources () {return NewList<apiresource> { //Defining API Resources here, if you use the constructor to pass in name, a scope with the same name is created by default.//This needs to be noted, because if the API does not have scope, it cannot be accessed at all NewApiresource {Name="Chatapi", DisplayName="Chat API", Apisecrets= {NewSecret ("123123". SHA256 ())}, Scopes={ NewScope ("Chatapi","Chat API") } } }; } Public StaticIenumerable<identityresource>identityresources () {return NewList<identityresource> { NewIdentityresources.openid (),Newidentityresources.profile ()}; }
The migration method is increased in 2.StartUp, and migration (app) is called in Configure. Wait ();
Public Static AsyncTask Migration (Iapplicationbuilder app) {using(varScope =app. Applicationservices.createscope ()) {//Migrating Demodbcontext ContextsScope. Serviceprovider.getrequiredservice<demodbcontext>(). Database.migrate (); //Migrating Persistedgrantdbcontext ContextsScope. Serviceprovider.getrequiredservice<persistedgrantdbcontext>(). Database.migrate (); varConfigurationdbcontext = scope. Serviceprovider.getrequiredservice<configurationdbcontext>(); //Migrating Configurationdbcontext ContextsconfigurationDbContext.Database.Migrate (); //inject user management to add users varUsermanager = scope. Serviceprovider.getrequiredservice<usermanager<demouser>>(); foreach(varUserinchseeddata.users ()) { if(Usermanager.findbynameasync (user. UserName). Result = =NULL) { awaitUsermanager.createasync (User,"123123"); } } //Add apiresources identityresources clients if(!ConfigurationDbContext.ApiResources.Any ()) ConfigurationDbContext.ApiResources.AddRange (Seeddata.ap Iresources (). Select (R=r.toentity ())); if(!ConfigurationDbContext.IdentityResources.Any ()) ConfigurationDbContext.IdentityResources.AddRange (S Eeddata.identityresources (). Select (R=r.toentity ())); if(!ConfigurationDbContext.Clients.Any ()) ConfigurationDbContext.Clients.AddRange (Seeddata.clients (). Select (R=r.toentity ())); awaitConfigurationdbcontext.savechangesasync (); } }
There's a hole in the pit, add-migration command to create the migration code, you need to put migration (app). Wait () Comment out, why? This command will actually launch the application, and execute the code inside, when the first execution, the database does not exist, the migration code does not exist, so the data structure does not exist, if the implementation of the seed data operation will certainly fail, of course, add-migration itself is a code generator, input development operations , so this is a pit that does not count as pits.
4. Start the program, the console can see the migration information,
Well, the database has been created for us.
Look at Aspnetusers, user data is fine.
Well, this is it, and the next one begins to write the chat room backend service.
IdentityServer4 + SignalR Core +RABBITMQ build Web Instant Messaging (ii)