newbie to containers and coming back to asp.net in nearly a decade and i assumed this would be answered somewhere already but can't seem to locate it ! I have a simple asp.net app running on 3 nodes, the login is per node apparently and is lost when the request is served by a different node. i assumed there would be distributed session management tutorial but coming up short here, any pointers/how-to ?
FYI - in dev for now but for a public facing cloud solution azure/linode/... running 3 pods
The configuration/logging information is attached below
Redis is working on the localhost - not in a docker container
the behaviour is as follows
- say user is logged into pod1, whenever request hits pod1, it shows logged in,
- if the request hits pod2/pod3, it shows not logged in !!!
It looks like something else is required to make the session use redis !!!
public void ConfigureServices(IServiceCollection services)
{
var cx = Configuration["RedisCache:ConnectionString"];
var redis = ConnectionMultiplexer.Connect(cx);
services.AddDataProtection().PersistKeysToStackExchangeRedis(redis, "DataProtectionKeys");
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(20);
});
services.AddControllersWithViews();
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseDeveloperExceptionPage();
//app.UseExceptionHandler("/Error");
app.UseHsts();
}
if (env.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
// Adds session middleware to pipeline
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
APPSETTINGS.JSON
{
"ConnectionStrings": {
"DefaultConnection": "Server=host.docker.internal;Database=SAMPLE;Trusted_Connection=false;user id=XXX;password=XXX;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"RedisCache": {
"ConnectionString": "host.docker.internal:6379,ssl=True,abortConnect=False"
},
"AllowedHosts": "*"
}
I have the following logging information In Index.cshtml.cs
public void OnGet()
{
var str = $"({Environment.MachineName}) || {HttpContext.Session.Id} || {DateTime.UtcNow.ToString("hh:mm:ss")}: Current({User.Identity.Name})";
_logger.LogWarning(str);
string sessionKey = "testKey1";
HttpContext.Session.SetString(sessionKey, str);
}