1

I use dotnet core 5 and Identity for authorization in my website. I have a custom class User for my Identity.

This is my ConfigureServices:

 services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));
            services.AddDefaultIdentity<User>(e =>
                {
                    e.Password.RequireDigit = false;
                    e.Password.RequireLowercase = false;
                    e.Password.RequireNonAlphanumeric = false;
                    e.Password.RequireUppercase = false;
                    e.Password.RequireLowercase = false;
                    e.Password.RequiredUniqueChars = 2;
                    e.Password.RequiredLength = 5;
                    e.SignIn.RequireConfirmedPhoneNumber = false;
                    e.SignIn.RequireConfirmedEmail = false;
                }).AddRoles<IdentityRole>().AddRoleManager<RoleManager<IdentityRole>>()
                .AddDefaultTokenProviders().AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddControllersWithViews();

and in my Configure I have:

app.UseAuthentication();
app.UseAuthorization();

This is my SignIn Controller:

[ValidateAntiForgeryToken]
        [HttpPost("/Login")]
        public async Task<IActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result =
                    await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, true);
                if (result.Succeeded)
                {
                    return Redirect(model.ReturnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "Invalid Usename or password");
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

But Even if Model.RememberMe is true, the SignIn is not persistent and User Needs to signin every 20 minutes. How can I solve this problem?

  • in addition to the answer below, please check if your data protection keys are the same, this is especially important if the application is hosted on multiple instances. (running inside docker containers for example), i had a problem with this a while ago and it took me some time to figure this out – Isparia May 04 '21 at 07:48
  • I am not using DataProtection – AmirHossein Parsapour May 05 '21 at 03:42

1 Answers1

0

Try to set ExpireTimeSpan of cookie.Here is a sample to set expiretime to 1 day:

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(1);
});
Yiyi You
  • 16,875
  • 1
  • 10
  • 22