Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <Kendo>(Configuration.GetSection("kendo"));
            services.Configure <Theme>(Configuration.GetSection("theme"));
            services.Configure <Logo>(Configuration.GetSection("logo"));
            services.Configure <ChartGoalPatientPageInfo>(Configuration.GetSection("chart-goal-patient-page-info"));
            services.Configure <ChartHomepageInfo>(Configuration.GetSection("chart-homepage-info"));

            var settingsLaywGmail = Configuration.GetSection("layw-gmail-data").Get <MailData>();

            services.AddSingleton(settingsLaywGmail);

            var settingsDoctorAccount = Configuration.GetSection("doctor-account").Get <DoctorAccount>();

            services.AddSingleton(settingsDoctorAccount);

            var settingsServerIP = Configuration.GetSection("server-ip").Get <ServerIP>();

            services.AddSingleton(settingsServerIP);

            var settingsJsonStructure = Configuration.GetSection("json-structure").Get <JsonStructure>();

            services.AddSingleton(settingsJsonStructure);

            var settingsDb = Configuration.GetConnectionString("layw-db");

            services.AddSingleton(settingsDb);

            var settingsHomepageChartInfo = Configuration.GetSection("chart-homepage-info").Get <ChartHomepageInfo>();

            services.AddSingleton(settingsHomepageChartInfo);

            services.AddSingleton(new MQTTClient(Configuration.GetSection("mqtt-info").Get <MQTTInfo>(), settingsJsonStructure));

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })

            .AddCookie(options =>
            {
                options.LoginPath  = "/login";
                options.LogoutPath = "/signout";
            })

            .AddGoogle(options =>
            {
                var configuration    = Configuration.GetSection("google-codes").Get <OAuthCodes>();
                options.ClientId     = configuration.ClientId;
                options.ClientSecret = configuration.ClientSecret;

                options.Events = new OAuthEvents
                {
                    OnCreatingTicket = context =>
                    {
                        Uri apiRequestUri = new Uri("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + context.AccessToken);
                        dynamic result    = JsonConvert.DeserializeObject(APIUtils.Get(apiRequestUri.ToString()).ToString());

                        context.Identity.AddClaim(new Claim(ClaimTypes.Uri, (string)result.picture, ClaimValueTypes.String, "Google"));
                        return(Task.FromResult(0));
                    }
                };
            })

            .AddFacebook(options =>
            {
                var configuration    = Configuration.GetSection("facebook-codes").Get <OAuthCodes>();
                options.ClientId     = configuration.ClientId;
                options.ClientSecret = configuration.ClientSecret;

                options.Events = new OAuthEvents
                {
                    OnCreatingTicket = context =>
                    {
                        Uri apiRequestUri = new Uri("https://graph.facebook.com/me/picture?redirect&type=large&access_token=" + context.AccessToken);
                        dynamic result    = JsonConvert.DeserializeObject(APIUtils.Get(apiRequestUri.ToString()).ToString());

                        context.Identity.AddClaim(new Claim(ClaimTypes.Uri, (string)result.data.url, ClaimValueTypes.String, "Facebook"));
                        return(Task.FromResult(0));
                    }
                };
            });

            services.AddSession(options =>
            {
                options.Cookie.Name = ".Layw.Session";
            });

            var dbFactory = new AdminDataContextFactory(
                dataProvider: SQLiteTools.GetDataProvider(),
                connectionString: Configuration.GetConnectionString("layw-db")
                );

            services.AddSingleton <IDataContextFactory <AdminDataContext> >(dbFactory);
            SetupDatabase(dbFactory);

            services.AddSignalR();

            services.AddMvc();
        }