HtmlRedirect issue on ServiceStack 6.2

I just upgraded my project (ASP.NET Web with Razor - No MVC) from ServiceStack version 5.10 to 6.2 and the HtmlRedirect stopped working.

When I run the application it takes me to the default service(which secure) but not to the login screen. But, when I type “http://localhost/xyz/login” it takes me to the login screen.

Below is the stripped down version of the Configure method under AppHost:

public override void Configure(Container container)
{
	//Configure JsConfig
	
	SetConfig(new HostConfig
	{
		//...
		WebHostPhysicalPath = MapProjectPath("~/wwwroot"),
		//...
	});
	
	PreRequestFilters.Add((req, res) =>
	{
		//Pre Request Filters
		//...
	});
	
	this.GlobalRequestFiltersAsync.Add(async (httpReq, httpResp, requestDto) =>
	{
		//Global Request Filters
		//...
	});
	
	this.GlobalHtmlErrorHttpHandler = new RazorHandler("/oops");
	
	this.ServiceExceptionHandlers.Add((httpReq, request, exception) =>
	{
		//Handle Service Exceptions
		//...
	});
	
	Plugins.Add(new AuthFeature(() => new AppUserSession(),
		new IAuthProvider[] {
			new CustomCredentialsAuthProvider(),
			new CustomBasicAuthProvider()
		})
	{ HtmlRedirect = VirtualPathUtility.ToAbsolute("/login"),  GenerateNewSessionCookiesOnAuthentication = true, DeleteSessionCookiesOnLogout = true, });
}

VS: 2017
.NET Framework: 4.8
OS: Windows 10

We wont be able to repro the issue without the custom AuthProvider implementations, if you want to put together a stand-alone repro we can run locally to repro the issue we’ll be able to identify the issue.

Otherwise in your first registered CustomCredentialsAuthProvider can you add this custom failed authentication handling to perform the redirect on failed authentication attempts:

public override Task OnFailedAuthentication(IAuthSession session, IRequest httpReq, IResponse httpRes)
{
    var feature = HostContext.AssertPlugin<AuthFeature>();
    if (feature.HtmlRedirect != null && req.ResponseContentType.MatchesContentType(MimeTypes.Html))
    {
        var url = feature.GetHtmlRedirectUrl(req, feature.HtmlRedirect, includeRedirectParam:true);
        res.RedirectToUrl(url);
        return TypeConstants.EmptyTask;
    }
    return base.OnFailedAuthentication(session, httpReq, httpRes);
}

Thank you!

Your code fixed the issue. Can you give me directions or where to dig in further to understand what exactly the issue was?

Thanks.

I’d be debugging into the code to find out what’s preventing error handling from redirecting, easy in JetBrains Rider, but hopefully also possible in VS .NET with the SourceLink Enabled Packages.

1 Like