'Running a method on timer for the life of Session

I use the following code segmant to authenticate my users to office365 for my organization and create a session cookie. This also allows me to start an SMTP connection using the MailKit SMTP Client which I initiate as a singleton in the startup.cs configuration:

                        this.smtpClient.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

                    this.smtpClient.Authenticate(UserName, Password);

                    var claims = new List<Claim>
                    {
                        new Claim(ClaimTypes.Name, user.InternalUserNumber.ToString())
                    };
                    
                    var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

I would like to keep this connection open through the life of the session so I need to run :

smtpClient.noop();

This will allow me to keep the connection alive by pinging the server however I need to run this every 5 minutes in the background of the users session. I have seen questions on stackoverflow which may seem similar but they do not limit the lifetime to the session but instead are running a timer from IHostService.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source