Cookie authentication is working in local but no publised (ASP.NET Core 6.0 application)

45 views Asked by At

I have a problem with my cookie authentication, in localhost it is working good but when I publish it on a server I am disconnected after 40 seconds each time.

It seems like the expiration of cookies is configured.

So I really can't understand what can create this problem and only when the app is published on the server but no problem when testion on localhost.

here is part of my startup.cs:

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            services.AddDbContext<DataContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
            {
                options.ExpireTimeSpan = TimeSpan.FromMinutes(120);
                options.SlidingExpiration = true;
                options.LoginPath = new PathString("/Ouvriers/Login");
                options.AccessDeniedPath = new PathString("/Account/AccessDenied");
            });


        }

here is part of my controller :

public async Task<IActionResult> Connexion([Bind("Login,Mdp")] Ouvrier ouvrier)
        {
            Ouvrier account = await _context.Ouvriers.FindAsync(ouvrier.Login);

            if(!account.IsActif)
                RedirectToAction(nameof(incorrectPassWord));

            if (account != null)
            {
                string mdpHash = ConnexionModel.HashPassword(ouvrier.Mdp);

                if (account.Mdp == mdpHash)
                {
                    Authenticate(account.Login, account.IsChefAtelier);

                    return RedirectToAction("Details", "Ouvriers");
                }

                return RedirectToAction(nameof(incorrectPassWord)); 
            }

            return RedirectToAction(nameof(Login)); 
        }

        public async void Authenticate(string login, bool isChefAtelier)
        {
            List<Claim> users = new()
            {
                new(ClaimTypes.Name, login)
            };


            if (isChefAtelier)
                users.Add(new(ClaimTypes.Role, "ChefAtelier"));
            else
                users.Add(new(ClaimTypes.Role, "Ouvrier"));

            var claimsIdentity = new ClaimsIdentity(
                users, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                //AllowRefresh = <bool>,
                // Refreshing the authentication session should be allowed.

                ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(500),
                // The time at which the authentication ticket expires. A 
                // value set here overrides the ExpireTimeSpan option of 
                // CookieAuthenticationOptions set with AddCookie.

                //IsPersistent = true,
                // Whether the authentication session is persisted across 
                // multiple requests. When used with cookies, controls
                // whether the cookie's lifetime is absolute (matching the
                // lifetime of the authentication ticket) or session-based.

                //IssuedUtc = <DateTimeOffset>,
                // The time at which the authentication ticket was issued.

                //RedirectUri = <string>
                // The full path or absolute URI to be used as an http 
                // redirect response value.
            };


            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);
        }

If someone can please help me with this problem.

I can't understand it.

0

There are 0 answers