I am going through the Core User identity and trying to understand how the object gets injected in the Account Controller constructor. When the below constructor is called, objects are already instantiated through Dependency Injection. Is it done during the StartUp class "ConfigureServices" services.AddIdentity itself? can you please explain?
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();
You are correct, the AddIdentity
method is responsible for setting up the services. You can see this for yourself if you look at the code, this is a small snippet:
//etc...
services.TryAddScoped<UserManager<TUser>>();
services.TryAddScoped<SignInManager<TUser>>();
services.TryAddScoped<RoleManager<TRole>>();
//etc...