コード例 #1
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IRegisterRepository registerRep)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseSession();
            app.UseAuthorization();

            app.Use(async(context, next) =>
            {
                if (!context.Session.Keys.Contains("AuthReady"))
                {
                    context.Session.Set("AuthReady", "false");
                }
                await next.Invoke();
            });
            app.Use(async(context, next) =>
            {
                string CookieId;
                string Email;
                if (context.Request.Cookies.ContainsKey("CookieId")
                    &&
                    context.Request.Cookies.ContainsKey("Email"))
                {
                    if (context.Request.Cookies.TryGetValue("CookieId", out CookieId)
                        &&
                        context.Request.Cookies.TryGetValue("Email", out Email))
                    {
                        var user = registerRep.GetUserByCookie(int.Parse(CookieId), Email);
                        if (!(user is null))
                        {
                            context.Session.Set <RegisterModel>("AuthUser", user);
                            context.Session.Set("AuthReady", "true");
                        }
                    }
                }

                await next.Invoke();
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }