【问题标题】:Are my objects instantiated during ConfigureServices' AddIdentity startup class?我的对象是否在 ConfigureServices 的 AddIdentity 启动类期间实例化?
【发布时间】:2018-08-30 13:34:41
【问题描述】:

我正在查看核心用户身份并试图了解对象是如何被注入到 Account Controller 构造函数中的。当调用下面的构造函数时,对象已经通过依赖注入实例化了。它是在 StartUp 类“ConfigureServices”services.AddIdentity 本身期间完成的吗?你能解释一下吗?

帐户控制器

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();

【问题讨论】:

    标签: asp.net-core asp.net-core-identity


    【解决方案1】:

    你是对的,AddIdentity 方法负责设置服务。这个大家看the code就可以自己看,这是一个小sn-p:

    //etc...
    services.TryAddScoped<UserManager<TUser>>();
    services.TryAddScoped<SignInManager<TUser>>();
    services.TryAddScoped<RoleManager<TRole>>();
    //etc...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 2012-02-18
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      相关资源
      最近更新 更多