C
C#13mo ago
Adel

Logging out randomly

I made a car dealer website for my dad and never had any issues, but months down the road every time he trys to add a car it won't let him cause hes not logged in website just randomly kicks him out not sure what I need to do to fix this? Thank you my cookies? In my local testing nothing happens but only when I publish it it logs me out.
11 Replies
JakenVeina
JakenVeina13mo ago
you're gonna have to give us more than that $details
MODiX
MODiX13mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Adel
AdelOP13mo ago
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SurveyTest3.Models;
using System;

var builder = WebApplication.CreateBuilder(args);

// Add configuration
builder.Configuration.AddJsonFile("appsettings.json");

// Add services to the container.
builder.Services.AddControllersWithViews();

// Retrieve connection string
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
}
builder.Services.AddSingleton<ISurveyDBLayer>(new SurveyDBLayer(connectionString));
builder.Services.AddScoped<IAdminDBLayer>(provider =>
{
return new AdminDBLayer(connectionString);
});

// Add session configuration
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.SlidingExpiration = false; // Disable sliding expiration
options.ExpireTimeSpan = TimeSpan.FromMinutes(30); // Set the absolute expiration time
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax; // Adjust based on your needs
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

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

app.UseRouting();

app.UseSession(); // Add session middleware

app.UseAuthentication(); // Add authentication middleware
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SurveyTest3.Models;
using System;

var builder = WebApplication.CreateBuilder(args);

// Add configuration
builder.Configuration.AddJsonFile("appsettings.json");

// Add services to the container.
builder.Services.AddControllersWithViews();

// Retrieve connection string
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
if (string.IsNullOrEmpty(connectionString))
{
throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
}
builder.Services.AddSingleton<ISurveyDBLayer>(new SurveyDBLayer(connectionString));
builder.Services.AddScoped<IAdminDBLayer>(provider =>
{
return new AdminDBLayer(connectionString);
});

// Add session configuration
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.SlidingExpiration = false; // Disable sliding expiration
options.ExpireTimeSpan = TimeSpan.FromMinutes(30); // Set the absolute expiration time
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax; // Adjust based on your needs
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

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

app.UseRouting();

app.UseSession(); // Add session middleware

app.UseAuthentication(); // Add authentication middleware
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
Wasnt sure if it would be my program.cs or my controller I log in works great but like I said its random when it logs me out but its quick wont last more then 5mins
JakenVeina
JakenVeina13mo ago
you need to better understand what's causing you to "log out" is the cookie being wiped out by something? is your app rejecting it for some reason? if you can't replicate this behavior in Development, your best bet is to add an enable as much logging as you can, for the auth system, and examine those after-the-fact in Production
Mayor McCheese
Mayor McCheese13mo ago
He's literally saying log me out after 30 minutes since he's not extending the cookie options.SlidingExpiration = false; // Disable sliding expiration options.ExpireTimeSpan = TimeSpan.FromMinutes(30); // Set the absolute expiration time
JakenVeina
JakenVeina13mo ago
right, be he also claims he's getting logged out after about 5 minutes
Mayor McCheese
Mayor McCheese13mo ago
I missed that part
Adel
AdelOP13mo ago
I'm not sure what to add to catch these errors I made this website a year ago so my knowledge is very limited Wasn't sure if I should catch the error in the controller or program.cs
Mayor McCheese
Mayor McCheese13mo ago
If you remove the false on sliding expiration, what happens? iirc with sliding expiration set to false, on round trip the cookie isn't extended
Adel
AdelOP13mo ago
I'll test that out when I'm back home working atm Thank you guys for trying to help
Want results from more Discord servers?
Add your server