Home > OS >  Ending session on browser close throws Exception
Ending session on browser close throws Exception

Time:03-05

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

enter image description here

In case any of you have faced this issue and found a solution , please let me know.

CodePudding user response:

According to Microsoft documentation found here:

When the user closes a browser window or tab, or navigates to a new page or refreshes the page, the SignalR connection immediately ends because SignalR client code handles that browser event for you and calls the Stop method.

Perhaps the signalR client is automatically stopping the connection before you are finished working in the Logout method? It would then make sense why the error only happens when you close the tab.

CodePudding user response:

When the browser is closed, the TCP connection is closed and the HTTP request is aborted.

ABP uses the HttpContext.RequestAborted CancellationToken via HttpContextCancellationTokenProvider.

To override that behaviour in this case, call CancellationTokenProvider.Use(CancellationToken.None) in a using statement and await your async method calls:

public async Task LogOut()
{
    using (CancellationTokenProvider.Use(CancellationToken.None))
    {
        var user = _userManager.GetUserById((long)AbpSession.UserId);
        await _userManager.UpdateAsync(user);
    }
}

Reference: https://github.com/aspnetboilerplate/aspnetboilerplate/.../CancellationToken_Tests.cs#L56

  • Related