Add missing snippet to sesson doc (#17269)
* Add missing snippet to sesson doc * Add missing snippet to sesson docpull/17270/head
parent
dde1182cb5
commit
9c044f0bb2
|
@ -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.
|
||||
|
||||
|
|
|
@ -17,11 +17,14 @@ namespace Web.Extensions
|
|||
return value == null ? default : JsonSerializer.Deserialize<T>(value);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
|
||||
namespace Web.Extensions2
|
||||
{
|
||||
// Alternate approach
|
||||
|
||||
public static class SessionExtensions2
|
||||
public static class SessionExtensions
|
||||
{
|
||||
public static void Set<T>(this ISession session, string key, T value)
|
||||
{
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue