콘솔 응용 프로그램에서 UserManager 및 RoleManager에 액세스하려면 어떻게합니까?

asp.net asp.net-core c# entity-framework-core

문제

호스팅되지 않은 콘솔 응용 프로그램 패키지에서 UserManager 및 / 또는 RoleManger 에 액세스하려면 어떻게합니까?

수락 된 답변

Program.cs

public class Program
{
    public void Main(string[] args)
    {
        var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

        // do whatever
    }

    private readonly IServiceProvider serviceProvider;

    public IConfigurationRoot Configuration { get; private set; }

    public Program(IApplicationEnvironment env, IServiceManifest serviceManifest)
    {
        Configuration =
            new ConfigurationBuilder(Directory.GetCurrentDirectory())
            .AddJsonFile("config.json") // add the file to your project
            .AddEnvironmentVariables()
            .Build();

        var services = new ServiceCollection();
        ConfigureServices(services);
        serviceProvider = services.BuildServiceProvider();
    }

    private void ConfigureServices(IServiceCollection services)
    {
        var connectionString = Configuration["Data:DefaultConnection:ConnectionString"];

        // Register EntityFramework 7
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connectionString));

        // Register UserManager & RoleManager
        services.AddIdentity<ApplicationUser, IdentityRole>()
           .AddEntityFrameworkStores<ApplicationDbContext>()
           .AddDefaultTokenProviders();

        // UserManager & RoleManager require logging and HttpContext dependencies
        services.AddLogging();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }
}

config.json

public class Program
{
    public void Main(string[] args)
    {
        var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();

        // do whatever
    }

    private readonly IServiceProvider serviceProvider;

    public IConfigurationRoot Configuration { get; private set; }

    public Program(IApplicationEnvironment env, IServiceManifest serviceManifest)
    {
        Configuration =
            new ConfigurationBuilder(Directory.GetCurrentDirectory())
            .AddJsonFile("config.json") // add the file to your project
            .AddEnvironmentVariables()
            .Build();

        var services = new ServiceCollection();
        ConfigureServices(services);
        serviceProvider = services.BuildServiceProvider();
    }

    private void ConfigureServices(IServiceCollection services)
    {
        var connectionString = Configuration["Data:DefaultConnection:ConnectionString"];

        // Register EntityFramework 7
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connectionString));

        // Register UserManager & RoleManager
        services.AddIdentity<ApplicationUser, IdentityRole>()
           .AddEntityFrameworkStores<ApplicationDbContext>()
           .AddDefaultTokenProviders();

        // UserManager & RoleManager require logging and HttpContext dependencies
        services.AddLogging();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }
}



아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.
아래 라이선스: CC-BY-SA with attribution
와 제휴하지 않음 Stack Overflow
이 KB는 합법적입니까? 예, 이유를 알아보십시오.