RedirectIfNotAuthenticated missing from .Net Core

There use to be a property on the ViewPageBase that would redirect if the user was not authenticated. I am trying to redirect users from a default.cshtml page which doesn’t seem to trigger my general redirect code:

            GlobalRequestFilters.Add((req, res, requestDto) => {
            var sessionId = req.GetCookieValue("user-session");

            if (requestDto.GetType() == typeof(Authenticate))
            {
                return;
            }
            
            if (sessionId == null)
            {
                res.Redirect("login");
            }
        });

Any ideas on how to redirect unauthenticated users from controller-free pages?

Unlike most of ServiceStack’s multi-targeted .NETCore/.NET Framework code-base, ServiceStack.Razor for .NET Core is a completely different implementation based on MVC so a lot of existing APIs will be missing in the .NET Core implementation.

But I’ve added some missing APIs to the .NET Core impl in this commit which now includes:

This change is available from the latest v5.4.1 on MyGet.

Outstanding work Mythz, much appreciated.

I now get the following error when trying to run my solution

Unhandled Exception: System.MissingMethodException: Method not found: 'ServiceStack.Text.TextCase ServiceStack.Text.JsConfig.get_TextCase()'.
   at ServiceStack.ServiceStackHost.OnAfterInit()
   at ServiceStack.ServiceStackHost.Init() in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\ServiceStackHost.cs:line 237
   at ServiceStack.NetCoreAppHostExtensions.UseServiceStack(IApplicationBuilder app, AppHostBase appHost) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\AppHostBase.NetCore.cs:line 255

When reviewing the branch, I don’t see the method https://github.com/ServiceStack/ServiceStack.Text/blob/v5.4.1/src/ServiceStack.Text/JsConfig.cs

I do however see it is the master

Whenever you see an error like this it means you need to clear your NuGet packages cache to download the latest v5.4.1 pre-release packages.

That fixed the error. However it now doesn’t like to link to any of the embedded resources, claiming they don’t exist. I am using Rider and cleared all the nuget folders again but get this error, note it can’t find the ss-utils js file as well.

2018-12-25 19:17:39.314 -06:00 [Information] Failed to write error to response: {0}
System.IO.FileNotFoundException: Could not load HTML template embedded resource: /Templates/HtmlFormat.html
File name: 'HtmlFormat.html'
   at ServiceStack.Templates.HtmlTemplates.LoadTemplate(String templateName) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Templates\HtmlTemplates.cs:line 42
   at ServiceStack.Formats.HtmlFormat.SerializeToStreamAsync(IRequest req, Object response, Stream outputStream) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Formats\HtmlFormat.cs:line 101
   at ServiceStack.HttpResponseExtensionsInternal.WriteErrorToResponse(IResponse httpRes, IRequest httpReq, String contentType, String operationName, String errorMessage, Exception ex, Int32 statusCode) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\HttpResponseExtensionsInternal.cs:line 473
   at ServiceStack.HttpResponseExtensionsInternal.HandleResponseWriteException(Exception originalEx, IRequest request, IResponse response, String defaultContentType) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\HttpResponseExtensionsInternal.cs:line 355

Maybe this is what I get for trying to code over the holidays.

That’s a strange exception, I’ve just checked the MyGet package and can see the embedded resources exist:

And tested viewing the HTML Format in a new project (upgraded to use v5.4.1 packages) which works without issue, so I’m not sure what would cause this.

I think I will just wait till this code update makes it into the official nuget package . For reference, I am using Rider on a Mac. Thanks for all your exceptional help on this matter.

So I finally got it to work… It seems I hadn’t fully transitioned all of my code completely to using the new netCore bits. Once I did that, cleared all nuget caches and restored it was all happy.

1 Like