Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureContainer(ServiceRegistry services)
        {
            services.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true);
            services.AddRazorPages();

            // Build authentication configuration. Can't use fluent syntax because we want to support running without configurations in place.
            var authBuilder = services.AddAuthentication();

            var googleOptions = Configuration.GetSection("Authentication:Google");

            if (googleOptions.Exists())
            {
                authBuilder.AddGoogle(configureOptions =>
                {
                    configureOptions.ClientId     = googleOptions["ClientId"];
                    configureOptions.ClientSecret = googleOptions["ClientSecret"];
                });
            }

            var microsoftOptions = Configuration.GetSection("Authentication:Microsoft");

            if (microsoftOptions.Exists())
            {
                authBuilder.AddMicrosoftAccount(configureOptions =>
                {
                    configureOptions.ClientId     = microsoftOptions["ClientId"];
                    configureOptions.ClientSecret = microsoftOptions["ClientSecret"];
                });
            }

            var twitterOptions = Configuration.GetSection("Authentication:Twitter");

            if (twitterOptions.Exists())
            {
                authBuilder.AddTwitter(configureOptions =>
                {
                    configureOptions.ConsumerKey         = twitterOptions["ConsumerKey"];
                    configureOptions.ConsumerSecret      = twitterOptions["ConsumerSecret"];
                    configureOptions.RetrieveUserDetails = true;
                });
            }

            // Load our custom services *after* Microsoft's so that our services win.

            // Bind options pattern classes.
            services.Configure <SendGridOptions>(Configuration.GetSection(SendGridOptions.SectionName));
            services.Configure <TwoFactorAuthenticationOptions>(Configuration.GetSection(TwoFactorAuthenticationOptions.SectionName));

            // Need to use a lamba to resolve the SqlConnection because trying to bind by type was going off into setter injection land.
            services.For <IDbConnection>().Use(_ => new SqlConnection(Configuration.GetConnectionString("DefaultConnection"))).Scoped();

            // Load Lamar registries in our DLLs. (These are our preferred way of resolving things that are not part of the framework; but anything
            // needing IConfiguration like above is more easily handled in this method.)
            services.Scan(s =>
            {
                s.AssembliesFromApplicationBaseDirectory(f => f?.FullName?.StartsWith("Fulgoribus.", StringComparison.OrdinalIgnoreCase) ?? false);

                s.LookForRegistries();
            });
        }
Example #2
0
        public void ConfigureContainer(ServiceRegistry registry)
        {
            var containerConfig = ContainerConfiguration.CreateFromAssembly(typeof(Startup).Assembly);

            ServiceProvisioningInitializer.PopulateRegistry(containerConfig, registry);

            var dropboxLocator = new Container(registry).GetService <IDropboxLocator>();
            var dropboxPath    = dropboxLocator.LocateDropboxPath();

            var completePath = Path.Combine(dropboxPath, "Apps", "LinkedInPoc", "Secrets.txt");
            var textLines    = File.ReadAllLines(completePath);

            registry.AddAuthentication(
                options =>
            {
                options.DefaultScheme          = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = "LinkedIn";
            })
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddLinkedIn("LinkedIn", options =>
            {
                options.ClientId     = textLines[0];
                options.ClientSecret = textLines[1];
                options.CallbackPath = new PathString("/signin-linkedin");

                options.SaveTokens = true;
                options.Scope.Clear();
                options.Scope.Add("r_liteprofile");
                options.Scope.Add("r_emailaddress");
                options.Scope.Add("w_member_social");

                options.Events.OnCreatingTicket = ticket =>
                {
                    // For some reason, HttpContext.GetTokenAsync("access_token") doesn't work;
                    LinkedInAccessTokenSingleton.Value = ticket.AccessToken;
                    return(Task.CompletedTask);
                };
            });

            registry.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(
                                                             Configuration.GetConnectionString("DefaultConnection")));
            registry.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores <ApplicationDbContext>();
            registry.AddControllersWithViews();
            registry.AddRazorPages();
        }