예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var appSettings = configuration.GetSection(nameof(AppSettings)).Get <AppSettings>();

            services.AddSingleton(appSettings);

            string conn = configuration.GetConnectionString("Identity");

            services.AddDbContext <AppIdentityDbContext>(options =>
            {
                options.UseMySql(configuration.GetConnectionString("Identity"));
            });

            services.AddIdentity <AppUser, IdentityRole>(options =>
            {
                options.Password.RequireNonAlphanumeric = false;
                options.User.RequireUniqueEmail         = true;
                options.SignIn.RequireConfirmedEmail    = true;
            })
            .AddEntityFrameworkStores <AppIdentityDbContext>()
            .AddDefaultTokenProviders();

            var builder = services.AddIdentityServer()
                          // this adds the operational data from DB (codes, tokens, consents)
                          .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = options => options.UseMySql(configuration.GetConnectionString("Identity"));
                // this enables automatic token cleanup. this is optional.
                options.EnableTokenCleanup   = true;
                options.TokenCleanupInterval = 30;   // interval in seconds
            })
                          .AddInMemoryIdentityResources(ResourceConfig.GetIdentityResources())
                          .AddInMemoryApiResources(ResourceConfig.GetApiResources())
                          .AddInMemoryClients(ResourceConfig.GetClients(""))
                          .AddAspNetIdentity <AppUser>();

            services.AddSingleton <IEmailSender, EmailSender>(s => new EmailSender(appSettings.EmailConfig));

            if (environment.IsDevelopment())
            {
                builder.AddDeveloperSigningCredential();
            }
            else
            {
                throw new Exception("need to configure key material");
            }

            services.AddControllersWithViews();
        }