Blazor:没有会话/JWT令牌时重定向到“登录"页面? [英] Blazor: Redirect to Login page when there is no session / JWT token?

查看:1436
本文介绍了Blazor:没有会话/JWT令牌时重定向到“登录"页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Blazor中创建一个新应用程序,并且正在进行身份验证.我正在使用存储在本地存储中的JWT令牌.当应用程序加载时,我需要检查存储中是否有令牌.如果是这样,请将其添加到所有API请求的HTTP标头中,如果不是,则在页面加载之前将其重定向到登录页面..我该在哪里做?有多个页面需要进行此检查,因此需要在单个位置进行以覆盖所有内容.应该在启动程序中完成吗?

I am trying to create a new application in Blazor and am working on authentication. I am using a JWT tokens that is stored in Local storage. When the application loads i need to check if there is a token in the storage. If so, add it to the HTTP headers for all API requests, if not then redirect to the login page before the page loads....... where do i do that? There are multiple pages that need this check so this needs to be done in a single location to cover all. Should this be done in the Startup?

搜索没有提供解决方案,因为我是Blazor的新手,所以找到合适的搜索词有点困难:)

Searching did not provide a solution and since i am new to Blazor finding the right search terms is a bit difficult :)

推荐答案

Startup是什么意思? Startup类,对吗?

What do you mean Startup ? The Startup class, right ?

目前,Razor组件框架没有应用程序事件周期.请耐心等待...即将到来...同时,为了学习起见,请使用Components事件周期. 通常,您应该将JWT存储在本地存储中,包括有关用户的数据,用户是否已通过身份验证等数据,以及检索此数据以确定用户是否通过身份验证的服务.您可以在需要身份验证(或授权)时调用此服务.

Right now the Razor Components framework does not have application event cycles. Be patient...it is coming... Meanwhile, for learning's sake use Components event cycles. Generally, you should store yourJWT in the local storage, including data about the user, whether he has been authenticated and such like, and a service which retrieves this data finding out if the user is authenticated. You can call this service whenever authentication (or authorization) is necessary.

顺便说一句,您并不是要通过说"before the page loads"来停止...的过程.您只是说您不希望显示新个人资料页面"(例如,在在线约会网站中),如果用户未通过身份验证,对吗?好吧,在这种情况下,请从页面(组件)的OnInit或OnInitAsync方法中调用身份验证的本地服务(该服务从本地存储中检索数据).

Incidentally, you don't mean to stop the process of ... by saying "before the page loads". You just mean that you don't want the "New Profile Page", as for instance, in online dating website, to be displayed, if the user is not authenticated, right ? Well, in that case, call the authenticating local service (which retrieves data from the local storage), from the page (Component) OnInit or OnInitAsync methods.

希望这对您有帮助...

Hope this helps...

这篇关于Blazor:没有会话/JWT令牌时重定向到“登录"页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆