preface
Originally planned to share a few more technical articles during the Spring Festival, but the last one did not come out, lazy? Sort of, the process is like this: every time I take out my computer, it’s always sunny in my grandma’s yard, I can’t see the screen clearly, and it’s a little cold when I go back to my house (trying to make excuses), so I take my baby for a walk. There are not so many reasons, just want to steal a lazy; But after a few days to the computer composition of the principle of a simple lead, after sorting out and then separate to small partners to share it.
IdentityServer4 has been using in-memory data in test demonstrations, and will definitely be stored in the database in a formal project (unless the data is fixed and implemented in-memory). Let’s talk about persistent IdentityServer4 using the Demo.
The body of the
EF Core can be persisted with very little code. If you don’t know EF Core, please take a few minutes to read the article I shared on EF Core. NetCore EF Core NetCore
The main nuget packages are as follows:
- IdentityServer4: This package is the core package of IdentityServer4 and must be introduced whenever IdentityServer4 is used in a project;
- IdentityServer4. EntityFramework: this package is IdentityServer4 EF for packaging bag, support the use of EF for data persistence;
- Microsoft. EntityFrameworkCore. Essentially: according to the different database into different EF package, used here is to provide the local database of Microsoft, so the introduction of essentially;
- Microsoft. EntityFrameworkCore. Tools: if you need to program the package management console to migrate, just need to install this package in the specified assemblies;
- Microsoft. EntityFrameworkCore. Design: if you need to migrate in the command line, just need to install this package in the specified assemblies;
Note: Demo uses the way of package management console for migration operation; The idea on the command line is the same, but the commands used are slightly different;
The Demo went up
-
Project preparation
Instead of coding from start to finish, copy the Authorization Code from the previous section and focus on persistence (which is a good reason).
Source code address: github.com/zyq025/IDS4…
-
Began to lu code
First, introduce all the packages into the program, as shown below:
In the startup. cs file, change the memory mode to read from the database. What matters most are the two contexts encapsulated in IdentityServer4:
- ConfigurationDbContext: This context is for configuration data, such as clients, Resources, etc.
- PersistedGrantDbContext: PersistedGrantDbContext is used for user authorization data and temporary data, such as authorized data and tokens.
In the code, you only need to specify the database for these two contexts. If there is no custom requirement, no additional configuration is required, as follows:
-
The migration is done
Now that the code is done, all that’s left is a few steps of EF migration (shown here on the package management console) :
Note: If there are multiple contexts in the program, migration will report an error if the context is not specified, because the program does not know which one to migrate to.
-
The new migration
Specify ConfigurationDbContext:
Specify PersistedGrantDbContext:
Brief description of parameters:
-Context: specifies the Context for migration.
-OutputDir: indicates the path for storing generated files.
The final file migration result is as follows:
-
According to the migration file, synchronize to the database
Synchronous ConfigurationDbContext first
At this point, the corresponding database is generated, and the corresponding configuration related table is created in the database:
Resynchronization PersistedGrantDbContext
This watch is small, only two:
In fact, it is already possible, as long as the corresponding table in the database to add the corresponding data, you can operate; The novice partner must be distressed, so many tables, how to know which table save what data ah; Don’t worry, let’s put the original memory of the data stored in, and then look at each table stored what, this is more clear, than a table said is also direct, this way can also encourage small partners to start, otherwise do not know what stored inside (ha ha ha, clever). That’s the fourth step, which is……
-
-
It’s nice to add some seed data
The seed data here refers to the original data in memory, read them out to the database on the line; Very simple, through the above corresponding directly save, the code is as follows:
Configure (); Configure ();
Start the authorization server, resource server, and client and go through the process to see if it is successful. If everything goes well, then go back and see what data is stored in each table. It is really clear. Here is not a screenshot said, I believe that small partners can understand.
There must be a small partner to talk to this, the user is still using the memory; The reason why it is not mentioned above is that the control of the user is completely in our own hands, we can design by ourselves (that is, users, menus, roles), or integrate Microsoft encapsulated Identity(the common user that has been encapsulated).
Here demo is sure to pick cool to play, integrate Microsoft package good Identity, to continue to polish code:
-
Introduce related Nuget packages:
A brief description of imported packages
IdentityServer4. AspNetIdentity: IdentityServer4 encapsulates the Indentity of the Microsoft support;
Microsoft. AspNetCore. Identity. EntityFrameworkCore: provide EF support for Identity;
-
Just do it:
Since IdentityDbContext is abstract and cannot be used directly, we need to define a subclass ApplicationDbContext for IdentityDbContext as follows:
Then register the related services in startup. cs:
-
Migration and synchronization: The output path is not specified here. Partners can specify it according to their own needs.
The related tables are generated in the database as follows:
-
Change login and logout logic;
Logout when call _signInManager. SignOutAsync (); Can be
-
Add seed data, that is, add user data when the program starts;
Configure (zoe123456&); Configure (zoe123456&)
Here is not specifically for Microsoft package Identity said so detailed, partners can refer to the document: docs.microsoft.com/zh-cn/aspne…
conclusion
Here’s how to get started with IdentityServer4. The next section starts with service discovery.
A handsome guy who was made stupid by the program, watch the “Code variety circle “, learn with me ~