Blazor: Redirect to Login page when there is no session / JWT token?

前端 未结 4 1046
失恋的感觉
失恋的感觉 2021-01-28 09:14

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

4条回答
  •  鱼传尺愫
    2021-01-28 09:41

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

    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.

    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...

提交回复
热议问题