I need my application set a cookie so that the user must not login every time. I have decided for a 365 days cookie. The cookie seems to work only in localhost and not when i publish the application online. I have checked with Chrome and the cookie is set in both cases correctly. The name of the cookie is ".AspNetCore.Identity.Application" and it is set to expire in one year, both in localhost and on the server online. The cookie remains there even when the Session is over or the application shuts down, but in localhost I remain logged-in while online not.
Here is my Startup code:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<ForumDbContext>(options =>
options.UseSqlServer(Configuration["ConnectionString"]));
services.AddIdentity<User, IdentityRole>(opts => {
opts.Password.RequireDigit = false;
opts.Password.RequiredLength = 7;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireNonAlphanumeric = false;
opts.SignIn.RequireConfirmedEmail = true;
}).AddDefaultTokenProviders().AddEntityFrameworkStores<ForumDbContext>();
services.ConfigureApplicationCookie(opts =>
{
opts.ExpireTimeSpan = TimeSpan.FromDays(365);
}) ;
services.AddAuthentication().AddCookie(options => {
options.LoginPath = "/account/login";
options.ExpireTimeSpan = TimeSpan.FromDays(365);
}) ;
services.AddSession(opt => opt.IdleTimeout = TimeSpan.FromMinutes(100));
services.AddSingleton<PathProvider>();
services.AddSingleton<BlackList>();
services.AddSingleton<HttpContextAccessor>(); //for the ISession
services.AddScoped<UserSession>(sp => UserSession.CreateInstanceByService(sp));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
var ci = new CultureInfo("it-IT");
ci.NumberFormat.NumberDecimalSeparator = ",";
ci.NumberFormat.CurrencyDecimalSeparator = ",";
ci.NumberFormat.CurrencySymbol = "€";
// Configure the Localization middleware
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(ci),
SupportedCultures = new List<CultureInfo>
{
ci
},
SupportedUICultures = new List<CultureInfo>
{
ci
}
});
app.UseSession();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
//Seeding the database
using (IServiceScope serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetService<ForumDbContext>();
var roleManager = serviceScope.ServiceProvider.GetService<RoleManager<IdentityRole>>();
var userManager = serviceScope.ServiceProvider.GetService<UserManager<User>>();
DbSeeder.Seed(Configuration, dbContext, roleManager, userManager);
}
}
}
I have also checked that I pass IsPersistent = true in
await signinManager.PasswordSignInAsync(user, loginModel.Password, true, false)