Escaping The .NET 8 Blazor Redirect To Login Hell

preview_player
Показать описание
The redirect to login functionality doesn't work in the .NET 8 Blazor Visual Studio template. That caused me a lot of frustration recently as I hit a lot of roadblocks trying to implement a redirect to login functionality in Blazor. As it turns out, things seem to be more similar to regular old style ASP.NET Core MVC apps than the previous Blazor version. However, I want to spare you the frustration I had and therefore here is everything you need to know on how to implement this very important functionality in .NET 8 Blazor SSR
#dotnet #aspnetcore #blazor

Join this channel to get source code access and other perks:

CONTENT
1. Intro: 00:00
2. Broken Blazor VS template: 00:25
3. Implement Blazor redirect to login
Рекомендации по теме
Комментарии
Автор

There are a few things I would like to point out in a pinned comment.

1. I didn't imply that the RedirectToLogin component in Routes.razor has a bug, but that it misleading. In earlier Blazor versions using a RedirectToLogin component was adopted as a fairly wide spread practice. Now when we see it there we think that "hey, nice. It redirect me if not authenticated". But it actually won't

2. Using the [Authorize] attribute in _Imports.razor would automatically redirect to login if not authenticated. But it would redirect to to /Accounts/Login because of the cookie default settings that I did tweak in this video. However, this redirection would happen due to the ASP.NET Core authentication mechanism, not because of the RedirectToLogin component in Routes.razor

3. This being said, I am aware that the RedirectToLogin component might sill be useful in interactive rendered components

4. Even tough there are some big changes in the programming model with this Blazor SSR, I think Blazor is on a very good journey and I think the Blazor team is doing a great work. It will only take a little bit of time for us developers to adapt to these changes. Change is not always easy.

Codewrinkles
Автор

Thank you for this information. It's funny that this is still a problem. I started a Blazor Web App Project recently and wondered why the template doesn't work... I mean, you made the video 9 months ago and they still didn't made hints about this and give you the same useless template... But really thank you, helped me and works like a charm.

kurokyuu
Автор

Thanks for your help. I found your video after losing an entire day figuring out this issue.

TheCodeCreator
Автор

authentication is such a black box on blazor still. MS needs to step it up more in .net 9 :| - but thanks for this. I was struggling with this exact thing last week.

caliberplays
Автор

Thank you, the old school mvc programming expertise paid off ;)

mouradaissani
Автор

if you set the Authorize attribute on the Home.razor it will redirect to login ? I can't see the problem? You may want some pages (like the login page) to be open and some some to require login

Lazzerman
Автор

What about [Authorize] junk in .net8, is this related?

derekgb
Автор

Love your videos, waiting for a beginning to end tutorial with best practices :)

E_G_
Автор

I think that will go to login if you click "Auth Required" in the menu if not mistaken. because that page required login to view.

RizaMarhaban
Автор

Do we handle this the same way if we're authenticating with Azure AD? I tried to implement what you suggest but I'm still not hitting the RedirectToLogin component

rorypayne
Автор

In one of previous videos you explained using old Razor Pages to handle user authentication to Blazor Server Application is the most right method to set right cookies and handle HttpContext on initial state. Is it still actual in NET8?

adam-xtte
Автор

@Codewrinkles can you please help me in redirecting to the return URL if given .

rashmitapiplani
Автор

thx!, on other question: i the previous version the NotFound Section in the Routes worked great. but now i got an default http error 404 page. Any Idea?

BartoYou
Автор

isn't the redirect supposed to work only when trying a page guarded by Authorize attribute? and it does that in the default template

luciannaie
Автор

Do you have of your codes on github about blazor 8 authentication

MohammadKomaei
Автор

WHen I try this, or any redirect from program.cs, I am getting a Console error of failing to load _framework/Blazor.web.js < unexpected token. This only happens with redirects from program. I cannot figure out why.

cmoever
Автор

next time make a bit of zoom please, it looks too small in cellphones

haroldpepete
Автор

@Codewrinkles Hi do you mind sharing ideas as to the best way to have your Blazor server app deployed on multiple nodes in an IIS Farm. Thank you for your time.

SamX-op