From 9c044f0bb226767199805e8ef956f7ab212c216c Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 10 Mar 2020 11:38:33 -0700 Subject: [PATCH] Add missing snippet to sesson doc (#17269) * Add missing snippet to sesson doc * Add missing snippet to sesson doc --- aspnetcore/fundamentals/app-state.md | 2 +- .../Extensions/SessionExtensions.cs | 7 +- .../samples/3.x/SessionSample/Startup4.cs | 67 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Startup4.cs diff --git a/aspnetcore/fundamentals/app-state.md b/aspnetcore/fundamentals/app-state.md index c2b404ce33..291bfa5780 100644 --- a/aspnetcore/fundamentals/app-state.md +++ b/aspnetcore/fundamentals/app-state.md @@ -89,7 +89,7 @@ To enable the session middleware, `Startup` must contain: The following code shows how to set up the in-memory session provider with a default in-memory implementation of `IDistributedCache`: -[!code-csharp[](app-state/samples/3.x/SessionSample/Startup.cs?name=snippet1&highlight=12-19,39)] +[!code-csharp[](app-state/samples/3.x/SessionSample/Startup4.cs?name=snippet1&highlight=12-19,39)] The preceding code sets a short timeout to simplify testing. diff --git a/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Extensions/SessionExtensions.cs b/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Extensions/SessionExtensions.cs index 41581f2e82..8b5035eb26 100644 --- a/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Extensions/SessionExtensions.cs +++ b/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Extensions/SessionExtensions.cs @@ -17,11 +17,14 @@ namespace Web.Extensions return value == null ? default : JsonSerializer.Deserialize(value); } } - #endregion + #endregion +} +namespace Web.Extensions2 +{ // Alternate approach - public static class SessionExtensions2 + public static class SessionExtensions { public static void Set(this ISession session, string key, T value) { diff --git a/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Startup4.cs b/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Startup4.cs new file mode 100644 index 0000000000..3599c57492 --- /dev/null +++ b/aspnetcore/fundamentals/app-state/samples/3.x/SessionSample/Startup4.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using SessionSample.Middleware; +using System; + +namespace SessionSample2 +{ + #region snippet1 + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.AddDistributedMemoryCache(); + + services.AddSession(options => + { + options.IdleTimeout = TimeSpan.FromSeconds(10); + options.Cookie.HttpOnly = true; + options.Cookie.IsEssential = true; + }); + + services.AddControllersWithViews(); + services.AddRazorPages(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + app.UseHsts(); + } + app.UseHttpsRedirection(); + app.UseStaticFiles(); + + app.UseHttpContextItemsMiddleware(); + + app.UseRouting(); + + app.UseAuthentication(); + app.UseAuthorization(); + + app.UseSession(); + + app.UseEndpoints(endpoints => + { + endpoints.MapDefaultControllerRoute(); + endpoints.MapRazorPages(); + }); + } + } + #endregion +}