Reputation: 3497
I am trying to implement basic authentication in my application. The users should be stored in a postgresql database.
I am able to create a user but I have no idea where it is stored. It is not stored in my database. But all created users are stored and accessible from the UserManager
. After a reboot the created users are still there. So it isn't stored in my database or in memory.
I used this part to enable identity with entity framework.
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<DatabaseContext>()
.AddDefaultTokenProviders();
The User
and Role
classes are just extending from IdentityUser
and IdentityRole
with Guid
as key type.
I use npgsql
for the database. I am pretty sure it is working because the migrations are migrated correctly:
var connectionString = Configuration.GetConnectionString("DatabaseContext");
services.AddEntityFrameworkNpgsql()
.AddDbContext<DatabaseContext>(options => options.UseNpgsql(connectionString));
For creating and getting users I inject Microsoft.AspNetCore.Identity.UserManager<User>
in the controller.
Why are the users not stored in my database?
Upvotes: 1
Views: 1021
Reputation: 17213
UserManager<TUser>
will look for an injected IUserStore<TUser>
. That's how it persists users and chats with your database. The default implementation of the user store can be found here. By default, this implementation looks to inject DbContext
directly.
Thing's I'd try:
IdentityDbContext
IUserStore
from your service collection and look at the Context
property to see what database context it's using.If you're absolutely sure that your users are not going into your database, then IUserStore<TUser>
must be using a different context to the one you're using. If that's the case, either delete the other context or add a more specific version of IUserStore
to your service collection. Ex: Replace IUserStore<TUser>
in your services with UserStore<TUser, IdentityRole, DatabaseContext, string>
. (Note that we're specifying DatabaseContext
there, instead of the base class DbContext
)
Upvotes: 1