Authentication not working with MVC

Hey guys,

Weird behavior I’m only seeing on a PC. I’ve got an ASP.NET Core 2.0 site that uses ServiceStack’s Authentication. My HomeController inherits from Authenticate and I believe the AppHost is setup correctly.When I run the app I get the error in the screenshot, he code is available here

edit: When I remove the Authenticate attribute the site loads ok :expressionless:

Thanks for the help!

There are a few issues, you can’t use ServiceStack’s Razor support with ASP.NET MVC, so since you’re using MVC Controllers you need to remove the RazorFormat plugin and any uses of the RazorHandler

//Remove ServiceStack Razor features
//Plugins.Add(new RazorFormat()); 
//this.CustomErrorHttpHandlers[HttpStatusCode.Unauthorized] = new RazorHandler("/login");

By default ServiceStack will redirect 401 Unauthorized Responses to AuthFeature.HtmlRedirect which redirects to a Login page at /login.

Another issue is that the HomeController is protected with [Authenticate] which also includes the ExceptionHandler page to also use the HomeController which requires an Authenticated UserSession and will prevent you from seeing any errors.

Ideally your Error Page should be in a separate controller that doesn’t have any filters, as a quick fix I just replaced it with the developer exception page.

// app.UseExceptionHandler("/Home/Error");

I did find an issue with HandlerFactoryPath = "api" requests which wasn’t being properly short-circuited that was causing an issue which I’ve resolved in this commit.

This change is now available from v5 that’s now available on MyGet. Please note some of the changes in v5. E.g. .NET Core NuGet packages no longer have the .Core suffix so your dependencies from NuGet would now look like:

<PackageReference Include="ServiceStack" Version="5.*" />
<PackageReference Include="ServiceStack.Mvc" Version="5.*" />
<PackageReference Include="ServiceStack.OrmLite" Version="5.*" />
<PackageReference Include="ServiceStack.OrmLite.Sqlite" Version="5.*" />
<PackageReference Include="ServiceStack.Server" Version="5.*" />

Cheers Demis, as always top class support mate.