RickStrahl / Westwind.AspnetCore.LiveReload

ASP.NET Core Live Reload Middleware that monitors file changes in your project and automatically reloads the browser's active page
Other
473 stars 42 forks source link

LiveReload and ASP.NET Core authentication not playing together nicely #36

Closed hsankala closed 4 years ago

hsankala commented 4 years ago

I've tried to used LiveReload with a visual studio vanilla ASP.NET core website with authentication. But after successfully authenticating on the login page the site is not successfully authenticating the logged in user on other pages

I've uploaded a the simple site is here:

https://github.com/hsankala/Vanilla.ASP.NET.Core.With.Auth/tree/master/Vanilla%20Login%20ASP.NET%20Core

Steps to reproduce

  1. Compile site, do a EF migration
  2. Run site
  3. Register User
  4. Click the dummy button to confirm the newly registered users email address
  5. Go to the login page
  6. Enter the email and password of the new account

    Authenitcation is successful in the backend -> redirected to home page but not logged in

7.) Comment out the app.UseMvcWithDefaultRoute(); in Startup.cs. Try steps 3-6 again login successful and shows user login on home page

RickStrahl commented 4 years ago

Not sure what causes that, but there should be no issue with this as nothing is done to the headers of the request. I run several applications here with cookie authentication and don't see any issues with that either - the authentication works fine.

Without more detail there's not much we can check on this end. Perhaps hook up Fiddler and take a look at the request traces to see what is actually failing. Also run with the component turned off via the settings and see if the problem actually doesn't exist without running with the middleware.

hsankala commented 4 years ago

I've turned off LiveReload in the appSettings and yes the problem still exists without running the middleware.

I tracked it down if you add the following line Configure method

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
....
app.UseMvcWithDefaultRoute();
...
 }

You then need to add the following lines in the ConfigureServices method otherwise you get an error

public void ConfigureServices(IServiceCollection services)
        {
         ..... 
            services.AddRazorPages().AddRazorRuntimeCompilation().AddMvcOptions(options => options.EnableEndpointRouting = false);
            services.AddMvc().AddRazorRuntimeCompilation().AddMvcOptions(options => options.EnableEndpointRouting = false);
         .....
        }

It's this is causing the issue, I think the readme instructions might be wrong Rick.

The readme says add:

app.UseMvcWithDefaultRoute();

but you get an error when you add this. The readme says then to add this

services.AddRazorPages().AddRazorRuntimeCompilation(); services.AddMvc().AddRazorRuntimeCompilation();

But this does not get rid of the runtime error, which is

System.InvalidOperationException: 'Endpoint Routing does not support 'IApplicationBuilder.UseMvc(...)'. To use 'IApplicationBuilder.UseMvc' set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices(...).'

If you update the AddRazorRuntimeCompilation to

            services.AddRazorPages().AddRazorRuntimeCompilation().AddMvcOptions(options => options.EnableEndpointRouting = false);
            services.AddMvc().AddRazorRuntimeCompilation().AddMvcOptions(options => options.EnableEndpointRouting = false);

It runs, but front end GUI doesn't think your logged in as was orginially reported. I'm not sure why .net core 3.1 does this it's not a problem with LiveReload but following the readme you get your knickers in a twist here.

Interestingly Rick your test project of Net 3.1 in the startup doesn't use

app.UseMvcWithDefaultRoute();

Startup.cs#L47

Is app.UseMvcWithDefaultRoute(); nessesary if we lose it everything works fine for me, if this is the case we just need to lose it from the readme instrucions.

RickStrahl commented 4 years ago

In .NET Core 3.1 and later you should use endpoint routing, so the correct syntax for this is:

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

In 2.x didn't have endpoint routing so the syntax is different.

When upgrading projects and in doubt, I recommend to always create a separate new project and compare how the default Startup.cs is set up compared to the old version...

RickStrahl commented 4 years ago

Going to close this since it appears this was a configuration issue.

hsankala commented 4 years ago

I think your readme instructions need to be updated. The readme says app.UseMvcWithDefaultRoute();

image

But if I understand correctly app.UseMvcWithDefaultRoute(); is used for 2.x. I think the readme needs to be updated to say this line is only needed for 2.x and not nessesary for .NET Core 3.1