I’m building an extension of the virtual file system that makes sure the user is authenticated to servicestack before serving the file, otherwise I want to throw a 401 error. This is working well except that error page is the ASP.NET “uncaught exception” page, I want to display the browser default 401 page instead.
public class AuthenticatedFileSystemMapping : FileSystemMapping
{
public AuthenticatedFileSystemMapping(string alias, string rootDirectoryPath) : base(alias, rootDirectoryPath)
{ }
public override IVirtualFile GetFile(string virtualPath)
{
//bad kitty if not authenticated
var req = HostContext.GetCurrentRequest();
if (!req.IsAuthenticated())
throw new HttpError(401, nameof(HttpStatusCode.Unauthorized), ErrorMessages.NotAuthenticated.Localize(req));
return base.GetFile(virtualPath);
}
}