コード例 #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            EmailTools.Replacements.AddRange(UrlMaker.GetTagReplacements());

            database = new Database(Configuration["ConnectionStrings:DefaultConnection"]);

            IEmailSender emailSender = new Office365Emailer(Configuration["Email:Address"], Configuration["Email:Password"], "Admin");
            Emailer      emailer     = new Emailer(emailSender, "");

            // add objects to initialise constructors with
            services.AddSingleton(typeof(Database), database);
            services.AddSingleton(typeof(Emailer), emailer);
            services.AddSingleton(typeof(IEmailSender), emailSender);
            services.AddTransient <ISmsSender, AuthMessageSender>();

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
            });

            // Require https sitewide
            services.Configure <MvcOptions>(options => { options.Filters.Add(new RequireHttpsAttribute()); });

            services.AddIdentity <User, Role>(o =>
            {
                o.User.RequireUniqueEmail = true;

                o.Lockout.MaxFailedAccessAttempts = 5;
                o.Lockout.DefaultLockoutTimeSpan  = TimeSpan.FromMinutes(5);
                o.Lockout.AllowedForNewUsers      = false;

                o.Password.RequireDigit           = false;
                o.Password.RequiredLength         = 6;
                o.Password.RequiredUniqueChars    = 1;
                o.Password.RequireLowercase       = false;
                o.Password.RequireNonAlphanumeric = false;
                o.Password.RequireUppercase       = false;
            })
            .AddUserStore <MyUserStore>()
            .AddRoleStore <MyRoleStore>()
            .AddDefaultTokenProviders();

            services.AddAuthentication()
            .AddGoogle(googleOptions =>
            {
                googleOptions.ClientId     = Configuration["Authentication:Google:ClientId"];
                googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
            })
            .AddMicrosoftAccount(microsoftOptions =>
            {
                microsoftOptions.ClientId     = Configuration["Authentication:Microsoft:ClientId"];
                microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
            })
            .AddFacebook(facebookOptions =>
            {
                facebookOptions.ClientId     = Configuration["Authentication:Facebook:ClientId"];
                facebookOptions.ClientSecret = Configuration["Authentication:Facebook:ClientSecret"];
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("CanAdmin", policy => policy.RequireClaim("IsAdmin"));
            });

            services.AddControllersWithViews().AddNewtonsoftJson();
            services.AddRazorPages();

            services.AddLogging(builder =>
            {
                builder.AddConfiguration(Configuration.GetSection("Logging"))
                .AddConsole()
                .AddDebug();
            });
        }