The Authenticate attribute is just a simple ServiceStack > attribute , meaning it works in both MVC and ServiceStack.
Applying this filter returns a 401 UnAuthorized response for all non-HTML requests. for example, if you called this using Ajax, you may find this error response and redirect to the client.
From v3.9.23 + from ServiceStack, the [Authenticate] attribute will automatically redirect all authentication errors to the default ~/login url.
You can redefine this URL when registering AuthFeature, for example:
Plugins.Add(new AuthFeature(...) { HtmlRedirect = "/path/to/my/login" });
which applies globally to all [Authenticate] attributes, or you can override this based on adhoc using:
[Authenticate(HtmlRedirect="/path/to/my/login")]
Note. Attributes are inherited, so you can add this once to the SecuredService class, and all subclasses inherit its behavior.
Manual redirection
To redirect a UnAuthorized HTML request manually, you can do your own validation + redirection with:
public object Secured(Request request) { if (!base.SessionAs<MyCustomSession>().IsAuthenticated) return new HttpResult(HttpStatusCode.Redirect, "Un Authorized") { Headers = { {"Location", "/path/to/login" } } }; }
There is also a DRY wrapper around the above redirect, which you can use instead:
public object Secured(Request request) { if (!base.SessionAs<MyCustomSession>().IsAuthenticated) return HttpResult.Redirect("/path/to/login"); }
mythz
source share