I am working on an Asp.net zero project with frontend in angular and backend in Asp.net core with database in MS SQL Server. I was working on a functionality by which I can call logout function on closing browser or tab to end the session.
Here is the Javascript Code to call logout on browser/tab close:
window.addEventListener("unload", function (e)
{
if (context.validNavigation == 0)
{
context._authService.logout();
}
});
Below is the C# function which works fine if we manually logout via menu click, but fails when we close browser:
public async Task LogOut()
{
if (AbpSession.UserId != null)
{
if (AbpSession.TenantId != null)
{
var user = _userManager.GetUserById((long)AbpSession.UserId);
if (user != null)
{
user.AdminLoginTime = null;
_userManager.UpdateAsync(user); **//This is where I get Task exception**
}
LoginInput loginInput = new LoginInput
{
EventId = (int)AbpSession.TenantId,
Count = _commonLookupAppService.CurrentloginCount().Result
};
this._loginHub.Clients.Group("login_" + AbpSession.TenantId).SendAsync("ReceiveLoginStatus", loginInput);
}
var tokenValidityKeyInClaims = User.Claims.First(c => c.Type == AppConsts.TokenValidityKey);
RemoveTokenAsync(tokenValidityKeyInClaims.Value);
var refreshTokenValidityKeyInClaims = User.Claims.FirstOrDefault(c => c.Type == AppConsts.RefreshTokenValidityKey);
if (refreshTokenValidityKeyInClaims != null)
{ RemoveTokenAsync(refreshTokenValidityKeyInClaims.Value);
}
if (AllowOneConcurrentLoginPerUser())
{
_securityStampHandler.RemoveSecurityStampCacheItem(
AbpSession.TenantId,
AbpSession.GetUserId()
);
}
}
}
The Task Exception is generated at _userManager.UpdateAsync(user) only when we close browser. If I do proper logout through menu button click, it works fine. The Task exception doesnt let record get updated in AbpUsers & SignalR Hub, also the exception exits the c# function. I am not able to resolve this issue on closing browser.
Stack Trace :
at Abp.Domain.Uow.UnitOfWorkInterceptor.<InternalInterceptAsynchronous>d__5`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ryt.Web.Controllers.TokenAuthController.<LogOut>d__37.MoveNext() in D:\Ravi_2021\sinyunpl\aspnet-core\src\ryt.Web.Core\Controllers\TokenAuthController.cs:line 343
In case any of you have faced this issue and found a solution , please let me know.