ServiceStack Razor Authentication

I look at an example of Rockstars and ServiceStack.Razor.

How do I configure authentication on, say, the secure.cshtml file. Therefore, I can redirect the user to Login.cshtml if necessary.

I only understand from the SocialBootstrapApi example, if I mix MVC hybird, I can put [authenticate ()] in the ServiceStackController to achieve this.

But what if I just want a clean SS project without .net MVC?

+7
source share
1 answer

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"); } 
+7
source

All Articles