I have a Blazor server-side project which I've been developing in Visual Studio 2019, using .NET5. All has been working fine.
I've just deployed the site to a test server (which has two such sites already running on it, so I know the server has everything it needs to run the site), but none of the Blazor stuff seems to work. Specifically...
[Authorize]
attribute is visible to anyoneDon't know what code to show, as it all works when running in VS. Here is the contents of App.razor
...
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<AuthorizeView>
<Authorized>
<LayoutView Layout="@typeof(MainLayout)">
<NotFound />
</LayoutView>
</Authorized>
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeView>
</NotFound>
</Router>
</CascadingAuthenticationState>
The _Imports.razor
file contains all the right using
s, specifically including...
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
Pages that are not supposed to be accessible to non-logged-in users are decorated with [Authorize(Roles = "Admin")]
in the code-behind or @attribute [Authorize(Roles = "Admin")]
in the .razor
file.
Anyone any ideas what I could have done wrong? Please advise if there is any more information I can give. Thanks
Update I just tried deploying the site to another domain on the same server, and it runs fine. I also tried pointing the other site to the folder for this one, and it worked fine (so it's not the deployed files), and then pointing this site to the other's folder, and it didn't work. So, it looks like there is something wrong with the site itself, but I ahve no idea what as it looks like the other.
It turned out that the problem was nothing to do with my code, nor my site settings on the server. I use Cloudflare, and had set it to minify HTML. This removes the two HTML comments that are essential for Blazor to work. See this article for more information.
So, if you are using Cloudflare, by all means set it to minify JavaScript and CSS, but DO not minify HTML.