我正在检查核心用户身份,并试图了解如何将对象注入到Account Controller构造函数中。调用以下构造函数时,对象已经通过依赖注入实例化了。是在StartUp类“ ConfigureServices” services.AddIdentity本身期间完成的吗?你能解释一下吗?
AccountController
public class AccountController : Controller
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly RoleManager<IdentityRole> _roleManager;
public AccountController(SignInManager<ApplicationUser> signInManager, RoleManager<IdentityRole> roleManager)
{
_signInManager = signInManager;
_roleManager = roleManager;
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();
答案 0 :(得分:0)
您是正确的,AddIdentity
方法负责设置服务。如果您查看the code,则可以自己看到,这是一个小片段:
//etc...
services.TryAddScoped<UserManager<TUser>>();
services.TryAddScoped<SignInManager<TUser>>();
services.TryAddScoped<RoleManager<TRole>>();
//etc...