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

相关内容

热门资讯

透视科技!hhpoker作弊实... 透视科技!hhpoker作弊实战视频(透视)一直真的是有挂(详细辅助切实教程)准备好在hhpoker...
透视ai代打(WEpoke)德... 透视ai代打(WEpoke)德州辅助(透视)详细辅助安装教程;建议优先通过德州辅助透明挂内置帮助文档...
透视安装!wepoker辅助工... 透视安装!wepoker辅助工具(透视)详细辅助黑科技教程(本来真的是有挂)在进入wepoker辅助...
透视科技!wejoker内置辅... 透视科技!wejoker内置辅助(透视)素来是真的有挂(详细辅助靠谱教程);致您一封信;亲爱wejo...
透视安卓版(wepOkE)微扑... 透视安卓版(wepOkE)微扑克规律(透视)详细辅助安装教程是一款可以让一直输的玩家,快速成为一个“...
透视透视挂!wpk透视辅助方法... 透视透视挂!wpk透视辅助方法(透视)详细辅助AI教程(果然真的有挂)1、全新机制【wpk透视辅助方...
透视透视!pokermaste... 此外,数据分析德州(pokermaster修改器)辅助神器app还具备辅助透视行为开挂功能,通过对客...
透视辅助(wepOke)wpk... 透视辅助(wepOke)wpk微扑克免费辅助(透视)详细辅助2025新版技巧;人气非常高,ai更新快...
透视智能ai!aapoker可... 透视智能ai!aapoker可以开挂吗(透视)详细辅助黑科技教程(原来是有挂)1、aapoker可以...
透视能赢!德州私人局怎么透视(... 透视能赢!德州私人局怎么透视(透视)先前真的是有挂(详细辅助攻略方法)准备好在德州私人局怎么透视ia...