通过 DbContext 访问与 UserManager 访问

Lor*_*ora 5 c# entity-framework-core asp.net-core asp.net-core-identity

使用 UserManager 或 DbContext 是否有缺点或优点?

如果我使用这个:

public class UserManager<TUser> : IDisposable where TUser : class

public virtual Task<TUser> FindByIdAsync(string userId);
Run Code Online (Sandbox Code Playgroud)

或者,如果我使用直接 dbcontext,例如:

                var user = dbContext.Users.Where(x => x.Id == model.Id).FirstOrDefault();
// Login like this
 await HttpContext.SignInAsync(..)
Run Code Online (Sandbox Code Playgroud)

Ali*_*son 4

今天,您从数据库中查询用户。如果您决定将身份验证委托给授权服务器怎么办?我以前见过这种情况:人们决定创建一个 Web API 来处理身份验证/授权详细信息。如果您使用的是DbContext,则必须在使用它的所有地方进行更改。

UserManager另一方面,通过使用,您只需更改您的实现UserManager即可使用HttpClientWeb API 来查询用户、角色和创建用户身份所需的其他内容。

通过和其他一些接口封装UserManager了实现细节。IUserStore我会避免直接查询任何身份表,即使它是非常试探性的。