浏览器关闭时结束会话会引发异常

发布于 2025-01-11 06:17:28 字数 3063 浏览 0 评论 0 原文

我正在开发一个 Asp.net 零项目,前端为 Angular,后端为 Asp.net core,数据库为 MS SQL Server。我正在开发一种功能,通过该功能,我可以在关闭浏览器或选项卡时调用注销函数来结束会话。

以下是在浏览器/选项卡关闭上调用注销的Javascript代码

window.addEventListener("unload", function (e) 
{
    if (context.validNavigation == 0) 
    {
      context._authService.logout();
    }
});

下面是C#函数,如果我们通过菜单单击手动注销,该函数可以正常工作,但当我们关闭浏览器时失败:

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()
                   );
                }
            }
        }

仅当我们关闭浏览器时,才会在 _userManager.UpdateAsync(user) 处生成任务异常。如果我通过单击菜单按钮正确注销,则效果很好。任务异常不允许记录在 AbpUsers 和 AbpUsers 中更新。 SignalR Hub,也是异常退出c#函数。 我无法在关闭浏览器时解决此问题。

堆栈跟踪:

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

“在此处输入图像描述”

如果你们中有人遇到过此问题并找到了解决方案,请告诉我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2025-01-18 06:17:29

根据微软文档发现 此处

当用户关闭浏览器窗口或选项卡,或者导航到新的浏览器窗口时
页面或刷新页面,SignalR 连接立即结束
因为 SignalR 客户端代码会为您处理该浏览器事件
调用 Stop 方法。

也许在您完成 Logout 方法中的工作之前,signalR 客户端会自动停止连接?这样就可以理解为什么只有在关闭选项卡时才会发生错误。

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.

莫多说 2025-01-18 06:17:29

当浏览器关闭时,TCP 连接将关闭,HTTP 请求将中止。

ABP 使用 <代码>HttpContext.RequestAborted CancellationToken 通过HttpContextCancellationTokenProvider

要在这种情况下重写该行为,请在 using 语句中调用 CancellationTokenProvider.Use(CancellationToken.None)await 您的异步方法调用:

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

参考:<一href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/8348c19895c50d87b654ce56ac367cb9a4e8822a/test/Abp.TestBase.SampleApplication.Tests/CancellationTokenProvider/CancellationToken_Tests.cs#L56" rel="nofollow noreferrer">https://github.com/aspnetboilerplate/aspnetboilerplate/.../CancellationToken_Tests.cs#L56

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文