BlazorserversideCookieAuthentication-directlinkworkevenuserisnotauthenticated
创始人
2024-12-21 15:30:25
0

在Blazor服务端应用程序中,当使用Cookie Authentication时,直接访问受保护的页面链接时,即使用户未经过身份验证,仍会成功访问页面。 为解决此问题,可以在页面加载时进行身份验证。 在具有身份验证要求的组件中实现IAsyncDisposable接口,并在DisposeAsync()方法中取消订阅身份验证事件。

下面是示例代码:

@page "/protected"
@inject AuthenticationStateProvider AuthenticationStateProvider

@if(ViewModel.IsAuthenticated) {

You have access to the protected content!

} else {

You don't have permission to access the protected content.

}
@code { private AuthenticationState AuthenticationState => AuthenticationStateProvider.GetAuthenticationStateAsync().Result; private protectedViewModel ViewModel; protected override async Task OnInitializedAsync() { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); ViewModel = new protectedViewModel(authState.User); } protected override async ValueTask DisposeAsync() { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); ViewModel.Dispose(authState.User); await base.DisposeAsync(); } public class protectedViewModel : IAsyncDisposable { private readonly IDisposable _subscription; public protectedViewModel(ClaimsPrincipal user) { _subscription = user.Identity.IsAuthenticated ? null : NavigationManager.NavigateToTaskAsync("/login", true).ContinueWith(t => (IDisposable)null).Result; } public bool IsAuthenticated => _subscription == null; public async ValueTask DisposeAsync() { if (_subscription is not null) { await ((NavigationManager)services.GetRequiredService()).NavigateToAsync("/login", true); _subscription.Dispose(); } } } }

相关内容

热门资讯

发现一款“wepoker辅助i... 您好:wepoker辅助ios的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
技巧知识分享“wepoker是... >>您好:wepoker是有透视挂的吗软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
今日公布“WePoKer到底有... 无需打开直接搜索加微信客服(136704302)咨询了解WePoKer到底有透视挂吗辅助器目前(微信...
科技介绍“wepoker能不能... 您好:wepoker能不能透视辅助这款游戏是可以开挂的,软件加【添加微信客服136704302】确实...
一分钟了解“wepoker到底... 无需打开直接搜索;操作使用教程:一分钟了解“wepoker到底能不能开挂”开挂辅助下载(透视)专业教...
科普常识“wepoker透视脚... wepoker透视脚本是一款可以让一直输的玩家,快速成为一个“必胜”的ai辅助神器,有需要的用户可以...
玩家科普“wepoker辅助器... 玩家科普“wepoker辅助器教程”开挂辅助下载(透视)可靠技巧是一款可以让一直输的玩家,快速成为一...
一分钟了解“wepokerpl... 一分钟了解“wepokerplus发牌规则”开挂辅助脚本(透视)第三方教程是一款可以让一直输的玩家,...
带你了解“WePoKer辅助透... >>您好:WePoKer辅助透视软件挂确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
交流学习经验“wepoker好... 您好,wepoker好牌率这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...