I am working on a Xamarin Apps. One of the requirements is to logout user after certain period of time. I put a timer that runs every 30 mins to tell the user that his session expired and give him option to continue his session or logout. If he decided to continue his session, the timer will continue to run every 30 minutes, but if he decided to end his session the timer will stop.
The timer in Xamarin doesn't honor the time you set on the StartTimer because it runs even before the 30 minutes.
App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new LoginPage());
}
LoginPage.xaml.cs
public LoginPage()
{
InitializeComponent();
CheckUserLoginStatus();
}
public async void CheckUserLoginStatus()
{
var msalService = new MsalAuthService();
if (await msalService.SignInAsync())
{
Settings.UserLastLogin = DateTime.Now;
App.Current.MainPage = new Master();
}
}
MsalAuthService.cs
public async Task<bool> SignInAsync()
{
try
{
// login logic here
//after successfull login start timer
Device.StartTimer(new TimeSpan(0, 30, 0), () =>
{
// do something every 30 minutes
Device.BeginInvokeOnMainThread(async () =>
{
// interact with UI elements
await AutoLogout();
});
var accessToken = ApplicationPropertyProvider.GetProperty<string>(Commons.AUTH_TOKEN);
return !string.IsNullOrEmpty(accessToken);
});
Application.Current.Properties["USERLOGINDATE"] = DateTime.Now;
return !string.IsNullOrEmpty(accessToken);
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}
}